0

I perform some data loading tasks from an Ojective§C class and once everything is loaded, I simply wants to display a Viewcontroller subclass prepared in a storyboard.

So when everything is ok, the following method is called:

- (void)loadingNextView
{
    CABBndGSite *mySite = [CABBndGSite alloc];

    CABBndGSelectLanguageViewController *vc = [[mySite myRootViewController].storyboard instantiateViewControllerWithIdentifier:@"SelectLanguageViewController"];
[[mySite myRootViewController] presentViewController:vc animated:YES completion:nil];

}

So I verified that myRootViewController is not nil. It's a UINavigationController class. vc is not nil so it found my view in the storyboard. Anyway, the presentViewcontroller message seems to doing what expected.

Certainly a stupid mistake but my poor iOS programming knowledge lets me in the fog!

I use this code from ViewController subclasses with success and as here I get a valid ViewController pointer, I don't understand why it doesn't work.

I also tried to implement the AppDelegate method explained here How to launch a ViewController from a Non ViewController class? but I get a nil navigation pointer. Maybe something not well connected in my application May I have some explanation?

Kind regards,

Community
  • 1
  • 1
fralbo
  • 2,534
  • 4
  • 41
  • 73

1 Answers1

1

UINavigationController maintains a stack of view controllers. You can access this stack through the viewControllers property. To present your view controller, you can:

  • (a) have the navigation controller push the new view controller on to the stack (pushViewController:animated:);

  • (b) have the top view controller in the view controller stack present the new view controller modally (presentViewController:animated:completion:), or;

  • (c) add the new view controller to the view controller stack array manually by assigning a new viewControllers array to the navigation controller's viewControllers property (setViewControllers:).

nzeltzer
  • 521
  • 4
  • 9
  • It seems I'm in the case a. Anyway I use presentViewController in other ViewController, so UI objects, successfully (I'm here in the third page). So can I use presentViewController in a ViewController, even if I try to display another VC for the first time? I tried to use pushViewController but AFAIR xCode told me that no pushViewController instance was available in this context. I'll check again ASAP. – fralbo Aug 31 '14 at 07:42
  • A navigation controller is required to "push" view controllers. If you "present" a view controller, that view controller doesn't get added to the existing navigation stack – it's just hanging out there on its own. Without passing judgment on your design, it sounds like you need to create a navigation controller with your "presented" view controller as root; "present" the _navigation controller_; then "push" your next view controller. Also, look at this previous question:: http://stackoverflow.com/questions/8044644/ios-pushviewcontroller-vs-presentmodalviewcontroller-difference – nzeltzer Aug 31 '14 at 19:52
  • So I understood what happens in my app: In fact I displayed the current viewcontroller with presentViewController and that is, the next ViewController wasn't displayed by presentViewController nor by pushViewController. This is I don't understand. Do I have to explicitly dismiss the one displayed by presentViewController before? – fralbo Sep 02 '14 at 08:46