WunDaii
WunDaii

Reputation: 2312

Check What Current rootViewController is?

I would like to check what the current rootViewController is.

I have a side menu viewController that slides out from the left of the screen and it displays 4 buttons - each point to a different viewController. When they are tapped, for example button1:

- (IBAction)button1Tapped:(id)sender {

    [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[myViewController1 alloc] init]]
                                                 animated:YES];
    [self.sideMenuViewController hideMenuViewController];
}

So I'm trying to do this:

  1. User is on myViewContollerX and opens the sideMenuViewController.
  2. On sideMenuViewController, buttonX is grey because the user was currently on myViewControllerX.
  3. User taps buttonY and myViewControllerY shows.
  4. On sideMenuViewController, buttonY is now grey because the user was currently on myViewControllerY.

So I'd need to check what the current rootViewController is, I assume. How would I do this? Thanks.

Upvotes: 0

Views: 194

Answers (3)

emotality
emotality

Reputation: 13045

How to check your current rootViewController and use it in an if statement :

// Get your current rootViewController
NSLog(@"My current rootViewController is: %@", [[[UIApplication sharedApplication].delegate.window.rootViewController class]);

// Use in an if statement
UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
if ([rootViewController isKindOfClass:[MyViewController class]])
{
    NSLog(@"Your rootViewController is MyViewController!!");
}

Upvotes: 2

Kyle Truscott
Kyle Truscott

Reputation: 1577

Not sure which side-panel library you're using, but perhaps you can just do the styling of the buttons when they're tapped. Like this:

- (IBAction)button1Tapped:(UIButton *)sender
{
  // .... set the center controller

  [self setButtonAsActive:sender];
}

- (void)setButtonAsActive:(UIButton *)activeButton
{
  for (UIButton *button in @[self.button1, self.button3, self.button3])
  {
    if (button == activeButton)
      // ... make it highlighted
    else
      // ... make it not highlighted
  }
}

Upvotes: 1

Simon McLoughlin
Simon McLoughlin

Reputation: 8475

You are using a setter to set contentViewController. Simply use a getter and read it.

e.g.

UIVIewController *contentViewController = self.sideMenuViewController.contentViewController;

NSLog(@"ContentViewController class: %@", [contentViewController class]);

you can check its class using:

if([contentViewController isKindOfClass: [UINavigationController class]])
{
    // check nav root and disable button
}

Note:

It sounds like it would be much easier to just disable the button when its clicked and enable all the other buttons, but I'm not sure if you need this info for another reason as well.

Upvotes: 0

Related Questions