1

I've got a project setup using a Storyboard that contains a UITabViewController as the initial root view. One of the tabs loads a NavigationController that in turn loads a custom view controller class.

From the custom view controller, I have a navigation bar button that I want to trigger an action that returns the root UITabViewController to it's first index. I've been able to do this using a traditional xib structure by adding the appDelegate class to the xib and linking a method to the button that way.

Effectively, I want the button to trigger code that looks something like this:

@implementation AppDelegate

@synthesize window = _window;
@synthesize tabBarController=_tabBarController;

-(IBAction)handleHome:(id)sender{
    //How do I send a message to the tabBarController?
    [self.tabBarController setSelectedIndex:0];
}

Is it possible to do this with the Storyboard approach? I looked at Segue's but that doesn't seem to be what I'm trying to do (there is no way for me to talk to the root UITabViewController from what I can see).

I've got the handeHome method being triggered using the Responder approach, so really all I need to know is how to access the instantiated tabViewController in the Storyboard.

Hopefully this question makes sense, let me know if there is anything I should expand on.

PrairieHippo
  • 347
  • 2
  • 12

2 Answers2

2

Why not just do this in your custom view controller?

- (IBAction)handleHome:(id)sender {
    self.tabBarController.selectedIndex = 0;
}

The tabBarController property is built in to UIViewController.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
1

I figured it out. I updated the quoted block of code to this:

@implementation AppDelegate

@synthesize window = _window;

-(IBAction)handleHome:(id)sender{

    UITabBarController *tabViewController = (UITabBarController *) self.window.rootViewController;

    [tabViewController setSelectedIndex:0];

}

Sigh... need more coffee before asking questions on SO

PrairieHippo
  • 347
  • 2
  • 12