Friday, July 23, 2010

Accessing your UITabBarController from another UIViewController

I recently came across a situation where I wanted to show the user a UIAlertView on a view listing data if there was no data entered yet, and then direct them to an 'Add' tab in my application after they clicked OK. I ended up doing this by accessing my application's delegate which then had access to the Tab Bar Controller of the entire application.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
//only one alertview so we'll just push here
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[[appDelegate tabBarController] setSelectedIndex:1];
}

The indexing is zero-based, as expected, so don't forget that :)

No comments:

Post a Comment