I have an application where I have 2 controllers, one is a viewcontroller which consists of a textfield and the other is a tableview controller. When I enter any value in the textfield of uiviewcontroller and get back using the navigation bar button item that value must be saved in the detailtextlabel of my previous tableviewcontroller.开发者_JS百科
Make property in appDelegate class
appDelegateview.h
NSString *text;
@property (nonatomic,retain) NSString *text;
AppDelegateview.h.m
@synthesize text
in viewWillDisappear of view.m
yourAppDelegateClass *obj=(yourAppDelegateClass *)[[UIApplication sharedApplication] delegate];
obj.text=@"your value";
in tableViewController.m
in cellForRowAtIndexPath:
yourAppDelegateClass *obj=(yourAppDelegateClass *)[[UIApplication sharedApplication] delegate];
cell.detailTextLabel.text=obj.text;
Pass a reference of a
NSString
varaiable to the 2nd view controller, change its value there, when your return to first view controller, you get it modified.Use
NSUserDefaults
to set the value in the 2nd view controller. When you come back to the first view controller, read the value in theviewDidAppear
method.
Thats pretty simple. Declare an NSString
object in your TableViewController.h
file like this:
@interface TableViewController.h {
NSString *detailTxt; }
Dont forget to retain and synthesize it.
Now open your other ViewController which contains TextBox. In that, create the object of TableViewController
like this:
TableViewController *nextController = [[TableViewController alloc] initWithNibName@"TableViewController" bundle:nil];
Now you have access to your TableViewController
objects. You can simply call the string you declared in TableViewController.h
and assign the text in the TextBox.
nextController.detailTxt = YourTextBoxName.Text;
Now go back to your TableViewController and assign the text in detailTxt
string to your cell.detailTextLabel
. Hope this helps.
Happy Coding!
Use delegate method for this. Send back the value (textfield value) to the first view controller (which contains tableview) through delegate method.
(U need to set the index of the row selected to the secondview controller and u have to send the index along with the value, so that u can update the datasource correctly).
Update the datasource when the value is received.
Reload the tableview in viewWillAppear of the first view controller.
Pre-req : You need to set the value from the datasource to the detailTextLabel.
精彩评论