Hi, guys, today will learn how to pass data from one view controller to another in iOS
1. First & foremost you have to analyse the type of data that is in your first ViewController that you have to pass ahead to the next view controller.
2. Suppose the data u have is in NSString type or NSDisctionary or let's suppose you have to pass the username i.e an NSString.
3. Let's take a basic example you have to pass the login data to next page so pass the username string ahead to next ViewController so first thing is u have to create an action of a button that takes us to the next view controller lets suppose - (IBAction)passData:(id)sender.
4. Now go to the .h file of the Second View Controller & create a property of the type of the data that is coming from First ViewController as for example we took a dictionary
or NSString so all you have to do is create a property of that very same type which is coming from your First ViewController so that it can easily contain the data.
5. Now import Second View Controller in First ViewController so that u can create an object of SecondViewController in the action of passData action.
6.Insert storyboard identifier of SecondViewController for example as “SecondVC”.
As shown below-
- (IBAction)nextBTN:(id)sender {
Second View Controller *SecondViewController = [self.storyboard instantiateViewControllerWithIdentifier:@SecondVC];
SecondViewController.StringTwo = StringOne;
[self.navigationController pushViewController: SecondViewController animated:YES];
}
7. Here StringOne contains the value that we want to pass & when the Storyboard will instantiate StringTwo will contain the passed value.You can check & verify it by checking the value in viewdidload function of the SecondViewController.
Thanks for reading
0 Comment(s)