I’ve just created second view controller class by extending from the UIViewController class. However, it doesn’t differ from the parent class until we add our own variables and methods. There are a couple of things we have to change:
- Assign a variable (textFromFirstView) for data passing – when user taps a button in the first view , there must be a way to pass the text to the second view.
- Assign a variable (textToShow) for the text label – presently the label is static. It should be updated as the text changes.
Okay, let’s add these two variables (textToShow and textFromFirstView). Select the “SecondViewController.h” and adds two properties for the interface:
@interface SecondViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *textToShow;
@property (strong, nonatomic) NSString *textFromFirstView;
@end
Now in the First view controller when a segue gets perform it automatically calls PrepareForSegue method. Inside this method create object of second viewcontroller and pass the value like this:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
SecondViewController *secondVC = [segue destinationViewController];
secondVC.textFromFirstView = @"This is raw text of First view Controller.";
}
And finally in the secondviewcontroller set the text string from the property on to the label.
Thanks
0 Comment(s)