UINavigationController push problem

Discussion in 'iOS Programming' started by erdinc27, Feb 19, 2016.

  1. erdinc27 macrumors regular

    Jul 20, 2011
    i use navigationController in the application that i develop. At a certain point i push a new controller to stack but i cant see it in navigation controller's controllers list but the new pushed view controller can be seen in the screen. Why navigation controller's stack return wrong array ?
    Here my code is
        self.mainNC = [DELEGATE.grabStoryboard instantiateViewControllerWithIdentifier:@"giveAdNC"];
        if (![Fucntions readPlistFile]) {
            [self.mainNC pushViewController:[DELEGATE.grabStoryboard instantiateViewControllerWithIdentifier:@"signInVC"] animated:YES];
    NSLog(@"TOP VIEW CONTROLLER %@", self.mainNC.viewControllers);
    SignInVc is the view controller class that i push and can be seen in screen but not in navigation controller's stack.
  2. Dookieman macrumors 6502

    Oct 12, 2009
    You need to tell your UINavigationController to push.

    Try this. [self.navigationController pushViewController:self.mainNC animated:YES];
  3. Mascots macrumors 68000


    Sep 5, 2009
    @dookie pretty much has it, but I wanted to clear something up in case it wasn't obvious:

    self.mainNC = [DELEGATE.grabStoryboard instantiateViewControllerWithIdentifier:@"giveAdNC"];
    When you are calling instantiateViewControllerWithIdentifier, you are not fetching the existing instance of your current Navigation Controller, instead you are creating an entirely new instance of it and attempting to push your view into that.

    See the notes for instantiateViewControllerWithIdentifier (emphasis mine):
  4. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    I recommend you use segue's to push view controllers. That will fix the bug Mascots mentions and should make your code cleaner.
  5. Dookieman macrumors 6502

    Oct 12, 2009
    This works for some simple things, but I feel that there is less and more straight forward code when using the pushNavigationController method. Everything can be accomplished in one method rather than two, that is if you need to pass data to the next viewController. No need for the prepareForSegue method at all.
  6. Mascots macrumors 68000


    Sep 5, 2009
    I use both depending on circumstance.

    I enjoy having a nicely connected Storyboard because that eliminates a lot of senseless logic and gives an overview of an apps frame, but other times I'll just peg storyboard for an instance and pass it myself, like when I build smaller view components that lack presence in SB or a reusable base.

    Or I can't get Storyboard to load not in XML format.
  7. JasmineJasmine macrumors newbie


    Feb 23, 2016
    I believe the problem here is that when you initialize a UIViewController by identify you only get the instance of that view controller and not the UINavigationController it is embedded in. To solve your problem you will need to instantiate and present the Navigation Controller (which embeds the CameraViewController). Although there are many ways to do this..
  8. erdinc27 thread starter macrumors regular

    Jul 20, 2011
    Thanks for answers guys. Still i am in trouble with the same problem. I try this one at last.
    UINavigationController *denemeNC = [self.storyboard instantiateViewControllerWithIdentifier:@"profilimNC"];
    SignInViewController *signInVC = [self.storyboard instantiateViewControllerWithIdentifier:@"signInVC"];
    [denemeNC pushViewController:signInVC animated:YES];
    and then present the navigation controller. But still same problem. SignInViewController can be seen on screen but when i write down its viewControllers then signInVc is not there
  9. Mascots macrumors 68000


    Sep 5, 2009
    Are you pushing a view into the current navigation stack, or are you attempting to show a new navigation controller that you can then present over your current one?
    Your code and descriptions are contradictory, so it's unclear what your goal is.

    If it's the former, you should be maintaining a reference or grabbing a reference to the current nav stack from the VC doing the work (not instantiating a new nav stack from Storyboard), and then push a newly instantiated sign-in VC through that existing reference.

    If it is the latter, you can either instantiate a _new_ navigation stack from Storyboard that already has the Sign-in VC as the root view controller (so you don't have to call the
    sign-in VC), and then present the navigation controller modally...
    OR instantiate your sign-in VC, manually create a new Navigation stack while setting the sign-I'm VC as the top view controller, and then present that newly created navigation stack.

Share This Page