简体   繁体   中英

Passing data between controllers gives an error due to complicated segue

I have a modal segue which goes from a view controller to a tab bar controller which is embedded in a table view as you can see from this screenshot:

I want to pass data between the login screen and the table view. I use the following code:

-(IBAction)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"Enter App"]){
    //Pass on username so that app can set everything up for that specific user
    GamesListTableViewController *gamesList = (GamesListTableViewController *)segue.destinationViewController;
    if ([segue.destinationViewController isKindOfClass:[GamesListTableViewController class]]) {
        NSLog(@"ENTERING PROPER CLASS");
    }
    else{
        NSLog(@"ENTERING WRONG CLASS");
    }
    gamesList.userID = self.userID;
}
}

My app crashes due to the last line of code. And i checked why (as you can see in the code above) and the reason is that the segue destination is not the GamesListTableViewController, but what i am assuming is the tab bar controller. How to i work around this problem?

UITabbarController *tabController = (UITabbarController*) segue.destinationViewController;
UINavigationController *nav = (UINavigationController*) tabController.viewControllers[0]; // or some other nr
GamesListTableViewController *tvc = nav.topViewController;

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