简体   繁体   中英

Set Method Values With prepareForSegue

Basically, i'm using the prepareForSegue method to transition to another view within my storyboard, my view had to be embedded in a navigation controller in order to display the navigation bar so I am passing variables in such a way:

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"exploreAddSelected"]){

        UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
        AddTableViewController *controller = (AddTableViewController *)navController.topViewController;
        controller.tagName = tagName;
    }
}

My issue is I need to be able to set some method values in the actual view controller when using the prepareForSegue method, as you can do when using pushViewController as below:

DetailViewController *detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
detailViewController.tagName = tagName;
[detailViewController setCurrentHashtag:nil];
[navigationController pushViewController:self.detailViewController animated:YES];

I need to be able to set the "setCurrentHashtag" method, that is declared in the view controller that the segue "exploreAddSelected" goes to. How would I do this?

The answer to this is actually extremely obvious, but it takes me to ask the question to realise the answer myself.

You can simply set:

controller.currentHashtag = nil;

Where currentHashtag is a newly declared instance in the view controller. Then in the actual view controller set:

[self setCurrentHashtag:currentHashtag];

Just add the currentHashtag to the method.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
  if([segue.identifier isEqualToString:@"exploreAddSelected"]){

    UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
    AddTableViewController *controller = (AddTableViewController *)navController.topViewController;
    controller.tagName = tagName;
    [controller setCurrentHashTag:nil];  //add this line
  }
}

I'm not sure what you are doing here. If the segue points to a view controller directly (and nor via another navigation controller), you do not need the mapping to topviewcontroller, but use the destinationcontroller directly.

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