可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm trying to select no tabs at all in my application. At first the first tab is selected, but I'd like to deselect it so no tabs at all would be selected.
Don't ask me why, it's just that way the client wants it! hehe
Thanks for your help!
PS: I already tried:
// rootController = UITabBarController
rootController.tabBar.selectedItem = 0;
rootController.tabBar.selectedItem = nil;
[rootController setSelectedIndex:[rootController.items objectAtIndex:0]];
[rootController setSelectedIndex:nil];
[rootController setSelectedIndex:0];
// That one works : (but I can't select 0 or -1 for instance)
[rootController setSelectedIndex:2];
Any ideas? Thanks again!
回答1:
You can deselect all tab bar items if you are using UITabBar instance without UITabBarController one.
In such case below code works well.
[tabBar setSelectedItem:nil];
If UITabBar is a part of UITabBarController then application will crash with exception:
'Directly modifying a tab bar managed
by a tab bar controller is not
allowed.'
In other words if you would like to get this worked you need to manage tabbar's routines manually without controller.
回答2:
I just came across this question and it's actually really simple:
tabBarController.selectedViewController = viewController;
This is somewhat similar to HG's answer, but setting the selected view controller to nil is unnecessary.
回答3:
I finally managed to do this using the following code:
DefaultView *defaultView = [[DefaultView alloc]initWithNibName:@"DefaultView" bundle:[NSBundle mainBundle]];
[self.tabBarController setSelectedViewController:nil];
[self.tabBarController setSelectedViewController:defaultView];
Note that just doing [self.tabBarController setSelectedViewController:nil];
won't do anything. You HAVE TO specify a view controller. This view Controller will be displayed with no tabBar icon selected. Upon selecting the other TabBar options, the defaultView will disappear and the required view will be loaded.
回答4:
Better to change selected image whenever you want & make a view hide or show according to your requirement. Here my piece of code that could help to understand:
-(void)viewWillAppear:(BOOL)animated{
if ([[NSUserDefaults standardUserDefaults]integerForKey:@"flagAsk"]) {
UITabBarItem *firstTab = [self.tabBarController.tabBar.items objectAtIndex:0];
firstTab.selectedImage = [[UIImage imageNamed:@"Ask2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
vieToHide.hidden=YES;
}
else{
UITabBarItem *firstTab = [self.tabBarController.tabBar.items objectAtIndex:0];
firstTab.selectedImage = [[UIImage imageNamed:@"Ask"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
vieToHide.hidden=NO;
}
}
回答5:
From the documentation:
This view controller is the one whose custom view is currently displayed by the tab bar interface. The specified view controller must be in the viewControllers array. Assigning a new view controller to this property changes the currently displayed view and also selects an appropriate tab in the tab bar. Changing the view controller also updates the selectedIndex property accordingly. The default value of this property is nil.
So, I would assume you need to [rootController setSelectedViewController: nil];
.
Update:
To clarify a bit,
[self.tabBarController setSelectedViewController:nil];
There is also documentation on preventing the selection of tabs that could be helpful.
回答6:
Are there better methods?
use [self.tabBarController setSelectedViewController:nil],
Warning : "-[UITabBarController setSelectedViewController:] only a view controller in the tab bar controller's list of view controllers can be selected."
回答7:
I think rootController.tabBar.selectedItem = 0;
it's wrong whatever you have tried.
Because when You are setting selectedItem=0
, then sure it will take the first tabBarItem of tabBarController
.