Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

TabBar and NavigationBar got hiden after adding pushViewController

I want to pass the variable " uid " between the FirstViewController and SecondViewController, so I used the following code to pass it. It successfully pass it, but the problem it hides the NavigationBar and the TabBar !

Any ideas on how I can solve it ?

  SecondViewController *vc2 = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
     vc2.uid = uid;
     [self.navigationController pushViewController:vc2  animated:YES];

9 Answers

Try something like this:

    UINavigationController *navigationController = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];

    SecondViewController * vc2 = (SecondViewController *)navigationController.topViewController;
    vc2.uid = uid;

    [self.navigationController pushViewController:vc2  animated:YES];

What is topViewController ?

The app crashes and says :

     [SecondViewController topViewController]: unrecognized selector sent to instance 0x1f223250 

topViewController is the viewController that is visible in the navigation stack. Your storyboard might look a bit different than what I first thought. Would you mind taking a screenshot of the storyboard?

hmm - I don't see any reason why you navigationBar will disappear. Try adding this in SecondViewController:

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated]; 
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

Still, the same issue. In my case I am passing from LoginViewController (UIViewController) to FirstViewController(UITabBarController) [Please see picture again]. I think there is a problem of communication between these two.

if you are using storyboards why not use a segue and implement prepare for segue to pass the variable?

I tried to do that, but in that case the uid is not passed.

I tried to do that, but in that case the uid is not passed.

can you show me what you used? also is the viewcontroller you are trying to pass the variable to embedded in a navigation controller?

I used this :

    -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
      {
      if ([segue.identifier isEqualToString:@"seg"]) {
      FirstViewController *vc2 = [segue destinationViewController];
      vc2.uid = uid;
      }
      }

The FirstViewController is a login screen with, 2 fields (email, password) after receiving the success login from my jSON file, it gives success, with the uid the idea here is that I want to pass this uid inside the app to use it in other tab, so that It shows the content depending on the uid. The viewcontroller is embedded in a navigation controller.

if it's embedded in a navigation controller do this:

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"seg"]) {
        UINavigationController *navigationController = segue.destinationViewController;
        FirstViewController *vc2 = (FirstViewController *)navigationController.topViewController;
        vc2.uid = uid;
    }
}