User
User

Reputation: 33

How can I switch to another tab in UITabBarController programmatically?

I have a UITableView and I want to switch to another tab upon clicking a cell in UITableView.

I have already tried that but it doesn't work

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

MainView *mainView = [[MainView alloc] initWithNibName:@"MainView" bundle:nil];
mainView.tabBarController.selectedIndex = 3;  

 }

Upvotes: 3

Views: 5092

Answers (3)

Adil Malik
Adil Malik

Reputation: 6357

[self.tabBarController setSelectedIndex:2];

Upvotes: 1

rob mayoff
rob mayoff

Reputation: 385600

You're loading a new copy of your MainView each time a row is selected. These new copies aren't displayed anywhere. You need to access the tab bar controller of your existing, displayed instance of your main view. Perhaps this will work:

self.tabBarController.selectedIndex = 3;

Upvotes: 15

trydis
trydis

Reputation: 3925

Have you checked that mainView.tabBarController isn't nil?

Upvotes: 1

Related Questions