认为NavigationController在真实设备上失败但在模拟器上失败?

时间:2015-02-26 23:08:36

标签: objective-c uinavigationcontroller segue uistoryboardsegue

这是我的代码,以便与另一个UINavigationController接近:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    static NSString *segueIdentifier = @"ShowDetails";

    if ([[segue identifier] isEqualToString:segueIdentifier]) {

        UINavigationController *navigationController = [segue destinationViewController];

        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];


        DetailViewController *detailViewController = (DetailViewController *)navigationController.topViewController;

        if ([detailViewController respondsToSelector:@selector(setGameID:)]) {
            detailViewController.selectedGameIdNumber = [NSString stringWithFormat: @"%ld", (long)indexPath.row];
            detailViewController.selectedSection = [self.sectionNames objectAtIndex:indexPath.section];
        }
    }
}

使用模拟器运行时一切正常。没有错误,数据被传递给下一个NavigationController。但是现在,当我尝试使用真实设备时,它会在此代码上失败:

DetailViewController *detailViewController = (DetailViewController *)navigationController.topViewController;

错误:

-[DetailViewController topViewController]: unrecognized selector sent to instance 0x14d649ca0
2015-02-27 00:06:09.446 TabbedTutorial[1029:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DetailViewController topViewController]: unrecognized selector sent to instance 0x14d649ca0'

我不知道为什么它可以在模拟器上运行但不在设备上运行。任何想法如何解决这个问题?

谢谢!

2 个答案:

答案 0 :(得分:0)

当您在不属于此实例的方法实例上调用选择器时。

答案 1 :(得分:0)

如果这个问题仍然不受欢迎,那么ASFAIK可能会在不同设备上发生不同的层次结构。

所以,你可能想尝试

     DetailViewController *controller ;

   if([segue.destinationViewController isKindOfClass:[UINavigationController class] ]  )
   {
       controller = (DetailViewController *) [[segue destinationViewController] topViewController];
   }
    else
    {
        controller = [segue destinationViewController];
    }

    // pass data 
}

希望这有帮助