简体   繁体   English

控制器之间传递数据会因错误的检测而导致错误

[英]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. 然后我检查了原因(如您在上面的代码中看到的),原因是segue的目的地不是GamesListTableViewController,而是我假设的是选项卡栏控制器。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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