简体   繁体   中英

How to pass data from one view controller to another view controller which is embedded in navigation controller

I was able to pass data from one view controller to another, but when I embedded viewpapersViewController in navigation controller, data is not passing, I want to pass data to second view controller when button is pressed

originally when button is pressed a request is sent, then in connectionDidFinishLoading

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

  // NSLog(@"Finish Loading");
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self->_responseData options:NSJSONReadingMutableLeaves error:&myError];


Papers *papersViewController =
[self.storyboard instantiateViewControllerWithIdentifier:@"Papers"];
papers = [[NSMutableArray alloc]init];
urls=[[NSMutableArray alloc]init];
for(id key in res) {

    NSString* name = [key objectForKey:@"name"];
    NSString* url =[key objectForKey:@"url"];

    [papers addObject:name];
    [urls addObject:url];

}

viewpapersViewController.exam = exam;
viewpapersViewController.papersName=papers;
viewpapersViewController.urls=urls;

[self presentViewController:viewpapersViewController animated:YES completion:Nil];

}

Suppose you want to pass a string,You can simply pass your data by these steps:-

1- Go on second view controller and take a -- NSString *str;

2- And when you are pressing a button from your first view controller directly pass the data
like : - secondClassObject.str = FirstClassString. 3- And then Push your Navigation simply.

Normally, we will pass data two ways:

  1. We just instantiate the target UIViewController and then pass the data to its properties.
  2. Another way we use is segue. Essentially, it's the same with the first way.

For your problem, what I could think of is you are not getting your UIViewController right. You can probably try to get the UINavigationController first. Then, use this property to access the top UIViewController: topViewController

If you have ViewController connected with same Navigation Controller . You must have performed PUSH Segue from 1st View to 2nd View by Drag from 1st to 2nd View. Click Push option in that . You have set up the Push Segue by this .

Give a name to identifier to segue by clicking on that link between the 2 Views .

Now you can perform simple Segue by

[self performSegueWithIdentifier:@"identifier for segue" sender:self];

Now in 1st View

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:@"identifier segue"]){
    // Get the new view controller using [segue destinationViewController].
    2nd View *vc = [segue destinationViewController];
    2nd vc.variableName  = YES;
}}

In 2nd View in interface file add property

@property variable *variableName;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM