I am creating a simple iPhone navigation based application. In this I am moving to second class from first class. In the second class I am setting some values to the string(selectedName). And when I go back to the first class in (void)ViewWillAppear, I am accessing the value of the selectedString by the object of that class. I have already allocated the object of second class in first class in –(void)ViewDidLoad method, but it logs null value there. Please help me out.
In second class i have a string named selectedString i m setting some values to it.and logs it ,it logs fine.
In first class viewDidLoad method i have allocated the object of second class like this
Second *obj=[Second alloc]initWithNibName:@"Second" bundle:nil];
in the viewWillAppear metode i m accesing the selectedString like this ,but it logs null.
NSLog(@"%@",obj.selectedString);
Try this for Passing Data Back:
To pass data back from ViewControllerB to ViewControllerA you need to use Protocols and Delegates.
To do this we will make ViewControllerA a Delegate of ViewControllerB, this allows ViewControllerB to send a message back to ViewControllerA enabling us to send data back.
For ViewControllerA to be delegate of ViewControllerB it must conform to ViewControllerB’s protocol which we have to specify. This tells ViewControllerA which methods it must implement.
1) In ViewControllerB.h below the #import but above @interface you specify the protocol.
2) next still in the ViewControllerB.h you need to setup a delegate property and synthesize in ViewControllerB.m
3) In ViewControllerB we call a message on the delegate when we pop the viewcontroller.
4) Thats it for ViewControllerB, now in ViewControllerA.h tell ViewControllerA to import ViewControllerB and conform to its protocol.
5) In ViewControllerA.m implement the following method from our protocol
6) The last thing we need to do is tell ViewControllerB that ViewControllerA is its delegate before we push ViewControllerB on to nav stack.