取消选择或取消选择iOS 5中标签栏中的所有标签

问题描述 投票:4回答:5

我是iOS开发的新手,我直接从IOS 5开始。我创建了一个故事板,其中包含一个tabview控制器作为其rootviewcontroller。我已经放了两个标签。

我想最初取消选择/取消选中所有标签。我该怎么做呢?我尝试了以下内容

 UIView *view = [[UIView alloc]initWithNibName:@"view" bundle:[NSBundle mainBundle]];
    [self.tabBarController setSelectedViewController:nil];
    [self.tabBarController setSelectedViewController:view];

我添加了标识符“view”的视图。

但这不起作用,它给出了错误:

 unrecognized selector sent to instance

我也试过以下

[self.tabBarController.tabBar setSelectedItem:nil];

但它说

'NSInternalInconsistencyException',原因是:'不允许直接修改由标签栏控制器管理的标签栏。'

我已在第一个选项卡的控制器中尝试了此代码。我想这样做是因为我想在第一个标签视图的顶部放置一个默认视图,并在下面任何一个标签上单击使用后隐藏它。

ios5 uitabbarcontroller uitabbar uitabbaritem unselect
5个回答
8
投票

我用它来清除任何选中的tabBarItems

[myTabBar setSelectedItem:nil];

4
投票

老问题。仅作为记录,如果Tab Bar由TabBarController管理,则无法取消选择所有选项卡。方法:

[self.tabBar setSelectedItem:nil];

仅当选项卡栏不是从选项卡栏控制器管理时才有效。如果是,则无法取消选择所有选项卡,必须始终选择一个选项卡。


1
投票

这有点不好意思。但我只是将-viewWillAppear中的色调颜色设置为灰色,当我希望它看起来没有选择时,然后在我想要显示选择时将色调颜色设置回“选定”颜色。

// since we cant turn off tabbar selection, change the color to make
// it look like nothing was selected.
self.tabBarController.tabBar.tintColor = [UIColor grayColor];  
-----------
// make sure tintcolor is turned on (just in case another module turned it off
self.tabBarController.tabBar.tintColor = self.view.tintColor;

0
投票

我找到了一个解决方案可能不是最好的方法,但它对我有用([self.tabBar setSelectedItem:nil];没有)。我将我的标签视图放在父视图(myTabBarViewContainer)中,然后当我想取消选择我的项目时,我在父视图上调用removeFromSuperview并重新初始化它:

[self.myTabBarViewContainer.view removeFromSuperview];

然后重新启动它和Voila!

不是很漂亮,但它有效......


-1
投票

您应该使用UITabBar。不是UITabBarController

取消选择或取消选择tabbar项swift 3.0

tabbar_Main.selectedItem = nil
© www.soinside.com 2019 - 2024. All rights reserved.