Ios 准备在模拟器上工作,但不在设备上工作

Ios 准备在模拟器上工作,但不在设备上工作,ios,objective-c,uinavigationcontroller,segue,Ios,Objective C,Uinavigationcontroller,Segue,我已经厌倦了让这一切顺利进行,所以我希望这里有人能帮我解决这个问题 我正在使用此代码切换到UINavigationController。此代码在模拟器上工作,但在真实设备上不工作: -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { static NSString *segueIdentifier = @"ShowDetails"; if ([[segue identifier] isEqu

我已经厌倦了让这一切顺利进行,所以我希望这里有人能帮我解决这个问题

我正在使用此代码切换到UINavigationController。此代码在模拟器上工作,但在真实设备上不工作:

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

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

        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

        UINavigationController *navigationController = segue.destinationViewController;
        DetailViewController *detailViewController = [navigationController viewControllers][0];


        detailViewController.selectedGameIdNumber = [NSString stringWithFormat: @"%ld", (long)indexPath.row];
        detailViewController.selectedSection = [self.sectionNames objectAtIndex:indexPath.section];
    }
}
在真实设备上,它会在以下位置崩溃:

 DetailViewController *detailViewController = [navigationController viewControllers][0];
detailViewController.selectedGameIdNumber = [NSString stringWithFormat: @"%ld", (long)indexPath.row];
错误如下:

-[DetailViewController viewControllers]: unrecognized selector sent to instance
 -[UINavigationController setSelectedGameIdNumber:]: unrecognized selector sent to instance 0x7fa1d273e8
然后我有了我试过的代码。此代码适用于设备,但不适用于模拟器:

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

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

        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

        DetailViewController * detailViewController = (DetailViewController *)segue.destinationViewController;


        detailViewController.selectedGameIdNumber = [NSString stringWithFormat: @"%ld", (long)indexPath.row];
        detailViewController.selectedSection = [self.sectionNames objectAtIndex:indexPath.section];
    }
}
在模拟器上,它在以下位置崩溃:

 DetailViewController *detailViewController = [navigationController viewControllers][0];
detailViewController.selectedGameIdNumber = [NSString stringWithFormat: @"%ld", (long)indexPath.row];
错误如下:

-[DetailViewController viewControllers]: unrecognized selector sent to instance
 -[UINavigationController setSelectedGameIdNumber:]: unrecognized selector sent to instance 0x7fa1d273e8
我是做错了什么,还是应该使用在真实设备上运行的第二个代码?
我希望有一个干净的解决方案,所以如果我做错了什么,请一定要开导我。谢谢大家。

问题在于如何访问视图控制器。设备上的视图控制器层次结构与模拟器上的不同,因此这两种解决方案都只在各自的场景中工作。当然,不同的不是模拟器,而是你正在模拟的设备是iPad

请注意,特别是对于SplitViewController,以及其他框架提供的视图控制器,控制器的功能可能会因目标设备而异。当您对视图控制器等的索引进行假设时,这种更改经常会让您感到痛苦


在模拟器上调试,在设备类型和视图控制器之间切换,我想你会明白我的意思。

我的解决方案是:创建一个通用结构,并通过函数调用强制转换自定义的viewcontroller。优点是您可以在任何需要的地方重用此函数。 虽然我的代码是用swift而不是Objective-C编写的,但我认为解决方法是一样的

struct DestinationController<T: UIViewController> {
    func get(segue: UIStoryboardSegue) -> T {
        if let navigation = segue.destinationViewController as? UINavigationController{
            return navigation.viewControllers.first as! T
        }
        return segue.destinationViewController as! T
    }
}

哪个模拟器/iOS版本,哪个设备/iOS版本?我怀疑答案是,您必须针对这两种情况编写防御代码,即使用isKindOfClass:测试目标视图控制器的类,并相应地处理每种情况。模拟器的部署目标设置为7.1,并且我的设备运行在IOS 7.1.1 iPhone 5s@pbasdfi上。模拟器是否也运行7.1?部署目标与正在模拟的设备不同?它说项目文件名@pbasdfI中的IOS SDK 8.1怀疑可能是iOS8-苹果改变了segues在iOS8中的工作方式。正如我之前建议的,为了适应这两种情况,请检查segue的destinationViewController类-如果是[UINavigationController类],则使用上面的第一块代码,如果是[DetailViewController类],则使用第二块代码。