Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segue not working with iOS6

excuse me if my question may sound stupid to some of you. I am relatively new to iOS Development but I have to port an app for iOS6. The app works perfectly on iOS 5 and XCode 4.3.2 but I am having a weird bug with Xcode 4.5 on the iPhone 6.0 simulator. After the initial screen is loaded I can hit any button I want, and I get the error:

 `Terminating app due to uncaught exception 'NSGenericException', reason: 'Could not find a navigation controller for segue 'register'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.`

The prepareForSegue method of the StartScreenViewController looks like this

if ([[segue identifier] isEqualToString:@"register"] || [[segue identifier] isEqualToString:@"forgotPassword"]) {
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:[segue destinationViewController]];
    appDelegate.window.rootViewController = navController;
}

Setting a breakpoint shows me that the method is still called corectly, but after the prepareForSegue is finished, the app crashes with the forementioned error. Could anybody point me into the right direction?

Thank you very much Martin

like image 251
Martin Friedrich Bauer Avatar asked Sep 29 '12 15:09

Martin Friedrich Bauer


2 Answers

You can say "the app was working perfectly", but the fact is that the code you cite was always very wrong, so it seem that this wrongness is now being exposed. You should never change the window's root view controller in the middle of the app; you should set it once at launch and never again. And, as Joshua has rightly said, preparing for a segue is not a time for you to mess with the view controller structure, which the segue itself about to mess with.

You've got a basket case on your hands. It might be a good idea for you to stop and understand iOS and view controllers (and storyboards) before you take another step, since you may end up ripping up the entire architecture of this app in order to get it right. Here's some help:

http://www.apeth.com/iOSBook/ch19.html

like image 83
matt Avatar answered Oct 19 '22 09:10

matt


Because you're using a push segue Cocoa is looking for an ancestral navigation controller in the hierarchy of StartScreenViewController.

  • If it finds one, it will use it to automatically push a new view controller onto that navigation controller's stack.
  • Since it's not finding one, your app is crashing.

prepareForSegue: exists for you to configure a VC that's about to get automatically displayed by iOS. It isn't your responsibility to display the VC, which is what you seem to be doing when you set window.rootViewController.

You may want to read through these docs: http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html

like image 5
Joshua Pokotilow Avatar answered Oct 19 '22 10:10

Joshua Pokotilow