问题
I have an app with a tab bar, which contains 5 tabs. I decided to have a "Home screen" instead of opening the app directly in the first tab. My home screen will contain a welcome message AND 5 buttons to segue directly to the wanted tab. Example: the 4th button will go to the 4th tab. After clicking one button, the home screen should never be seen again. The navigation mode will by the tab bar.
My Home View Controller 5 buttons and a reference to the Tab Bar Controller, to pass the button clicked. The Tab Bar Controller (@interface TabBar : UITabBarController <UITabBarControllerDelegate>
) controls the screen where the 5 tabs starts. For some reasons, I can't get [self.tabBarController setSelectedIndex:3];
to work. I put it in - (void)viewDidLoad
, but it does nothing.
Anyone has a clue why setSelectedItem isn't called?
EDIT: Look at that problem:
self.tabBarController.selectedIndex = 1;
NSLog(@"%i", self.tabBarController.selectedIndex);
At runtime, I get 0 as the result of NSLog... Why isn't the setter working?
回答1:
You can custom segue to the Tab Bar Controller and name the the segue (e.g. with "yourSegueName").
Than just call the UITabBarController and setSelectedIndex at the desired tab-page:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:@"yourSegueName"]){
UITabBarController *destController = [segue destinationViewController];
[destController setSelectedIndex:3];
}
}
来源:https://stackoverflow.com/questions/11726620/how-to-segue-from-uibutton-to-a-tab-bar-item