I am studying Object-C and I am writing a test app to learn.
This is an easy app, where in the first View i press a button, go in the second View (I used a curl animation)fill a text field and then press a button, pass the content of the field in the first view.
So I created the 2 Views, placed a button and a Label in the first View and linked the button from the first View to the second View using the right mouse click and drag a line with action Modal.
In the second View, I wrote the following code:
ViewController2.h
@property (weak, nonatomic) IBOutlet UITextField *firstTextField;
@property (strong, nonatomic) NSString *stringFromVC2;
- (IBAction)passTextToVC2Button:(id)sender;
ViewController2.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.firstTextField.delegate = self;
}
- (IBAction)passTextToVC2Button:(id)sender {
ViewController *VC2 = [self.storyboard instantiateViewControllerWithIdentifier:@"wb_ViewController"];
VC2.stringFromTextField1 = self.firstTextField.text;
[self presentViewController:VC2 animated:YES completion:nil];
}
The app works, but when I press the button from ViewController2, and go back to ViewController, it doesn’t animate the Curl animation, just it appears from the bottom of the page a new window (the ViewController) under the ViewController opened with the Curl animation.
I also tried to use:
[self dismissViewControllerAnimated:YES completion:nil];
instead of:
[self presentViewController:VC2 animated:YES completion:nil];
in this case, it closes the second view but the Label in the first View isn’t display with the text I inserted in the second View.
As I told you, I am studying so I am a beginner, please be patient.
The way to do this is to define a delegate protocol that your first view controller conforms to, and your second view controller can send a message to to pass back the value.
I’ve had to answer this question a few times so I created an very simple example project to show exactly what I mean. Download it and see how to pass data back down the controller stack.
Update
I’ve changed the sample to an new project for iOS6 with ARC and Storyboards.