Question

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?

Was it helpful?

Solution

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];

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top