2015-06-19 101 views
0

我是全新的ios開發,並與我正在嘗試做的打磚牆。ios準備for segue導致崩潰

我在我的方法中有這個代碼,這意味着從這個視圖傳遞一個特定的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 

任何一個人有任何想法,這可能是爲什麼?

非常感謝!

+1

你能後墜毀的消息?這是不可能的說什麼 – Miknash

+0

謝謝尼克,我已經添加了消息的問題 – Uberzen1

回答

1

從你的錯誤日誌和代碼我認爲你沒有在頂部的導航控制器。嘗試以下操作:

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

這工作完美,謝謝。這很奇怪,因爲我內置了導航控制器,所以我不知道爲什麼會出現這種情況? – Uberzen1

+0

好吧,我想你已經在視圖控制器調用show PDF之前嵌入了,我認爲「showPDF」調用者之後沒有新的導航控制器。 – Miknash

+0

我認爲你可能在那裏是正確的,無論如何,它現在正在按需要工作,所以謝謝你的幫助! – Uberzen1

1

您的segue直接連接到PDFViewController,而不是UINavigationController。如果這真的是你想要的,請更換

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

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

如果你想在彈出的PDF查看器,插入一個UINavigationController在故事板,使PDF視圖控制器的根視圖控制器並將切入類型更改爲模態。在這種情況下,您的代碼應該可以正常工

0

塞格斯目標視圖控制器不是導航控制器,但其PDFViewController你明確其類型轉換到UINavigation控制器