ios准备segue导致崩溃

时间:2015-06-19 10:53:10

标签: ios objective-c

我对ios开发并不陌生,并且我正在努力做到这一点。

我的方法中有这个代码,用于将特定的NSObject从此视图传递到下一个视图,但它会不断崩溃我的应用程序。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"showPDF"]){
    UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
    PDFViewController *controller = (PDFViewController *)navController.topViewController;
    controller.yourName = self.yourName;
}

我已经通过调试并确定它是第四行导致它崩溃。

输出:

2015-06-19 11:47:00.955 iOSPDFRenderer[2455:70005] -[PDFViewController topViewController]: unrecognized selector sent to instance 0x7c5375d0
2015-06-19 11:47:00.959 iOSPDFRenderer[2455:70005] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PDFViewController topViewController]: unrecognized selector sent to instance 0x7c5375d0'
*** First throw call stack:
(
    0   CoreFoundation                      0x01f2b466 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x01bb4a97 objc_exception_throw + 44
    2   CoreFoundation                      0x01f332c5 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
    3   CoreFoundation                      0x01e7bbc7 ___forwarding___ + 1047
    4   CoreFoundation                      0x01e7b78e _CF_forwarding_prep_0 + 14
    5   iOSPDFRenderer                      0x000ed00e -[ViewController prepareForSegue:sender:] + 238
    6   UIKit                               0x00985d77 -[UIStoryboardSegueTemplate _perform:] + 199
    7   UIKit                               0x00985e05 -[UIStoryboardSegueTemplate perform:] + 116
    8   libobjc.A.dylib                     0x01bca7cd -[NSObject performSelector:withObject:withObject:] + 84
    9   UIKit                               0x00368340 -[UIApplication sendAction:to:from:forEvent:] + 99
    10  UIKit                               0x003682d2 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
    11  UIKit                               0x0049ca56 -[UIControl sendAction:to:forEvent:] + 69
    12  UIKit                               0x0049ce73 -[UIControl _sendActionsForEvents:withEvent:] + 598
    13  UIKit                               0x0049c0dd -[UIControl touchesEnded:withEvent:] + 660
    14  UIKit                               0x003b8ffa -[UIWindow _sendTouchesForEvent:] + 874
    15  UIKit                               0x003b9ad5 -[UIWindow sendEvent:] + 791
    16  UIKit                               0x0037ebb1 -[UIApplication sendEvent:] + 242
    17  UIKit                               0x0038ebf6 _UIApplicationHandleEventFromQueueEvent + 21066
    18  UIKit                               0x00362bc7 _UIApplicationHandleEventQueue + 2300
    19  CoreFoundation                      0x01e4e98f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    20  CoreFoundation                      0x01e4449d __CFRunLoopDoSources0 + 253
    21  CoreFoundation                      0x01e439f8 __CFRunLoopRun + 952
    22  CoreFoundation                      0x01e4337b CFRunLoopRunSpecific + 443
    23  CoreFoundation                      0x01e431ab CFRunLoopRunInMode + 123
    24  GraphicsServices                    0x022da2c1 GSEventRunModal + 192
    25  GraphicsServices                    0x022da0fe GSEventRun + 104
    26  UIKit                               0x003669b6 UIApplicationMain + 1526
    27  iOSPDFRenderer                      0x000ec94d main + 141
    28  libdyld.dylib                       0x02e58ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

有没有人知道为什么会这样?

非常感谢!

3 个答案:

答案 0 :(得分:1)

您的segue直接与PDFViewController相关联,而不是UINavigationController。如果这真的是你想要的,请替换

UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
PDFViewController *controller = (PDFViewController *)navController.topViewController;

PDFViewController *controller = (PDFViewController *)segue.destinationViewController;

如果要在弹出窗口中输入PDF视图控制器,请在故事板中插入UINavigationController,使PDF视图控制器成为其根视图控制器,并将segue类型更改为模态。在这种情况下,您的代码应该可以正常工作。

答案 1 :(得分:1)

从您的错误日志和代码中我认为您的顶部没有导航控制器。请尝试以下方法:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"showPDF"]){
        PDFViewController *controller = (PDFViewController *))segue.destinationViewController;
        controller.yourName = self.yourName;
    }
}

答案 2 :(得分:0)

segues目标视图控制器不是导航控制器,但是它的PDFViewController并且您明确地将其类型转换为UINavigation控制器