Uberzen1
Uberzen1

Reputation: 415

ios prepare for segue causing crash

I'm brand new to ios development and have hit a brick wall with what I am trying to do.

I have this code in my method that is meant to pass on a specific NSObject from this view to the next view, however it keeps crashing my app.

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

I have debugged through and have identified that it is the fourth line causing it to crash.

Output:

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

Does any one have any ideas why this could be?

Many Thanks!

Upvotes: 0

Views: 124

Answers (3)

Ankit Sachan
Ankit Sachan

Reputation: 7850

segues destination view controller is not navigation controller but its PDFViewController and you are explicitly typecasting it to UINavigation Controller

Upvotes: 0

Miknash
Miknash

Reputation: 7948

From your error log and code I think that you don't have navigation controller at the top. Try following:

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

Upvotes: 1

Glorfindel
Glorfindel

Reputation: 22651

Your segue is connected directly to a PDFViewController, not a UINavigationController. If that is really what you want, replace

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

with

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

If you want the PDF view controller in a popup, insert a UINavigationController in the storyboard, make the PDF view controller its root view controller and change the segue type to modal. Your code should work correctly in this case.

Upvotes: 1

Related Questions