0

I have written this code and when I run the program it throws exception: unrecognized selector sent to instance 0xba3e750

 storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; navigationController = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewID"]; selected = navigationController.viewControllers[0]; 

when I put this line in comment

//selected = navigationController.viewControllers[0]; 

like that , the program doesn't throw exception. this is whole exception

2014-06-12 20:59:44.749 passing image[2194:a0b] -[SecondViewController viewControllers]: unrecognized selector sent to instance 0xba3e750 2014-06-12 20:59:44.753 passing image[2194:a0b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SecondViewController viewControllers]: unrecognized selector sent to instance 0xba3e750' * First throw call stack: ( 0 CoreFoundation 0x017445e4 exceptionPreprocess + 180 1 libobjc.A.dylib 0x014c78b6 objc_exception_throw + 44 2 CoreFoundation 0x017e1903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275 3 CoreFoundation 0x0173490b ___forwarding_ + 1019 4 CoreFoundation 0x017344ee _CF_forwarding_prep_0 + 14 5 passing image 0x000056a2 -[ViewController viewDidLoad] + 466 6 UIKit 0x00349318 -[UIViewController loadViewIfRequired] + 696 7 UIKit 0x003495b4 -[UIViewController view] + 35 8 UIKit 0x002719fd -[UIWindow addRootViewControllerViewIfPossible] + 66 9 UIKit 0x00271d97 -[UIWindow _setHidden:forced:] + 312 10 UIKit 0x0027202d -[UIWindow _orderFrontWithoutMakingKey] + 49 11 UIKit 0x0027c89a -[UIWindow makeKeyAndVisible] + 65 12 UIKit 0x0022fcd0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1851 13 UIKit 0x002343a8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824 14 UIKit 0x0024887c -[UIApplication handleEvent:withNewEvent:] + 3447 15 UIKit 0x00248de9 -[UIApplication sendEvent:] + 85 16 UIKit 0x00236025 _UIApplicationHandleEvent + 736 17 GraphicsServices 0x037932f6 _PurpleEventCallback + 776 18 GraphicsServices 0x03792e01 PurpleEventCallback + 46 19 CoreFoundation 0x016bfd65 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION + 53 20 CoreFoundation 0x016bfa9b __CFRunLoopDoSource1 + 523 21 CoreFoundation 0x016ea77c __CFRunLoopRun + 2156 22 CoreFoundation 0x016e9ac3 CFRunLoopRunSpecific + 467 23 CoreFoundation 0x016e98db CFRunLoopRunInMode + 123 24 UIKit 0x00233add -[UIApplication _run] + 840 25 UIKit 0x00235d3b UIApplicationMain + 1225 26 passing image 0x0000750d main + 141 27 libdyld.dylib 0x01e78725 start + 0 28 ??? 0x00000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

4
  • can you please add the whole error message from console. Commented Jun 12, 2014 at 17:09
  • it seems, that your ViewController "SecondViewController" isn't a UINavigationController. Commented Jun 12, 2014 at 17:14
  • property UIStoryboard *storyboard; property UINavigationController *navigationController; property SecondViewController *selected; Commented Jun 12, 2014 at 17:17
  • 1
    @Caleb sorry, i meant UINavigationController of course. My bad. Commented Jun 12, 2014 at 17:18

1 Answer 1

1

instantiateViewControllerWithIdentifier: returns a UIViewController, but you're storing it in a UINavigationController when you shouldn't. When you try to access a UINavigationController property (viewControllers), it crashed because UIViewController doesn't have a property named viewControllers.

Try storing the return of the method instantiateViewControllerWithIdentifier: in a UIViewController instead of a UINavigationController :

UIViewController * viewController = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewID"]; selected = viewController.navigationController.viewControllers[0]; 
Sign up to request clarification or add additional context in comments.

2 Comments

+1 There's no actual cast happening, but you're right that the OP is sending a UINavigationController message to a view controller that's not a nav controller.
no exception but the program don't work properly . There is a button when I click it that will go to a new view controller (selected)but in that controller there are no navigation buttons at the top of the screen.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.