Search code examples
iphoneios5uitabbarcontrollerstoryboarduitabbar

TabBarController setSelectedIndex Function Does Not Work


I have the same exact problem that appears in the following link

How to segue from UIButton to a Tab Bar Item?

and unfortunately I couldn't find any solution till now

Notes :

1) I'm using iOS 5 with a storyboard -if make a difference-

2) I've tried [self.tabBarController.tabBar setSelectedItem:[self.tabBarController.tabBar.items objectAtIndex:3]]; and also it does not work

3) I've tried to set IBOutlet for the tab bar and access it directly, but the app was crashed when I called [self.myTabbar setSelectedItem:[self.myTabbar.items objectAtIndex:3]]; and got this error : Directly modifying a tab bar managed by a tab bar controller is not allowed

enter image description here


Solution

  • OMG, it's like a magic, the solution is :

    [(UITabBarController*)self.navigationController.topViewController setSelectedIndex:3];
    

    I get the solution from the following post: https://stackoverflow.com/a/10533414/1150618