简体   繁体   English

prepareForSegue在模拟器上工作但不在设备上工作

[英]prepareForSegue working on simulator but not on device

I am so tired of trying to make this work so i hope somebody here will be able to solve this for me.. 我已经厌倦了尝试做这项工作,所以我希望有人能够为我解决这个问题。

I am using this code to segue to a UINavigationController. 我正在使用此代码来转换为UINavigationController。 This code WORKS on simulator but NOT on a real device: 此代码的工作在模拟器,但不是一个真正的设备上:

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

On the real device it crashes at: 在真实的设备上,它崩溃在:

 DetailViewController *detailViewController = [navigationController viewControllers][0];

with the error: 有错误:

-[DetailViewController viewControllers]: unrecognized selector sent to instance

Then i have this code that i tried. 然后我有我试过的代码。 This code WORKS on device but NOT on simulator: 此代码的工作对设备,但是不能在模拟器:

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

On the simulator it crashes at: 在模拟器上它崩溃在:

detailViewController.selectedGameIdNumber = [NSString stringWithFormat: @"%ld", (long)indexPath.row];

With the error: 有错误:

 -[UINavigationController setSelectedGameIdNumber:]: unrecognized selector sent to instance 0x7fa1d273e8

Am i doing something wrong or should i just run with the second code that works on real device? 我做错了什么或者我应该使用适用于真实设备的第二个代码? I want to have a clean solution so please do enlighten me if i am doing something wrong. 我想要一个干净的解决方案,所以如果我做错了什么请赐教。 Thank you all. 谢谢你们。

The issue is with how you are accessing the view controller. 问题在于您如何访问视图控制器。 On the device the view controller hierarchy is different from on the simulator, and so the two solutions both only work in their own scenario. 在设备上,视图控制器层次结构与模拟器上的不同,因此这两种解决方案都只能在自己的场景中工作。 Of course it isn't the simulator that is different, but rather the device you are simulating (guess is iPad) 当然不是模拟器不同,而是你正在模拟的设备(猜猜是iPad)

Be aware that, especially with splitViewcontrollers, but also with other framework provided view controllers that the functionality of the controllers may vary based on the target device. 请注意,特别是对于splitViewcontrollers,以及其他框架提供的视图控制器,控制器的功能可能会因目标设备而异。 This change often bites you when you make assumptions about indexes of view controllers and the like. 当您对视图控制器的索引等进行假设时,此更改通常会让您感到困惑。

Debug on the simulator, switching between device types and po the view controller and I think you will see what I mean. 在模拟器上进行调试,在设备类型之间切换以及查看控制器,我想你会明白我的意思。

Here's my solution: creating a generic struct and cast the customized viewcontroller by function call. 这是我的解决方案:创建一个通用结构并通过函数调用强制转换自定义viewcontroller。 The advantage is that you can reuse this function everywhere you need it. 优点是您可以在任何需要的地方重复使用此功能。 (Though my code is in written in swift rather than Objective-C, I think the way of solution is just alike) (虽然我的代码是用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
    }
}

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

相关问题 通过prepareForSegue传递数据只能在模拟器上使用,而不能在设备上使用 - Passing data via prepareForSegue works on simulator but NOT on device NSnumberFormatter在模拟器上工作,但不在设备上工作 - NSnumberFormatter working on simulator but not on device 可达性在模拟器上起作用,但在设备上不起作用 - Reachability working on simulator but not on device FileExistAtPath在模拟器上运行,但在设备上不运行 - FileExistAtPath working on Simulator, but not on Device TVOutManager 在模拟器上工作,但不在设备上? - TVOutManager working on Simulator, but not on a device? NSSearchPathForDirectoriesInDomains可在模拟器上运行,但不能在设备上运行 - NSSearchPathForDirectoriesInDomains working on simulator but not on device UIButton图像在模拟器上工作但不在设备上工作 - UIButton image working on simulator but not on the device Tabbarcontroller在模拟器上工作而不在iOS设备上 - Tabbarcontroller is working in simulator not on iOS device 约束动画无法在设备上运行,但可以在模拟器上运行 - Constraint animation not working on device but on simulator ECGraph不在设备上工作,但在模拟器上工作 - ECGraph not working on device, but works on simulator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM