问题描述
|
我有5个标签栏按钮的UITabBarController。在某些活动中,我想取消所有选项卡蝙蝠项目的显示。
有人可以帮忙吗?
谢谢,
安基塔
解决方法
首先,我想说的是,取消选择所有选项卡项是不好的用户体验。很有可能不会被应用商店接受。
我说完之后,在这里找到了答案。您可以接受此答案(如果可行!!!),但应向该用户提供道具。他在“键值观察”中使用了一个技巧,并使用了以下代码:
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Create the view controller which will be displayed after application startup
mHomeViewController = [[HomeViewController alloc] initWithNibName:nil bundle:nil];
[tabBarController.view addSubview:mHomeViewController.view];
tabBarController.delegate = self;
[tabBarController addObserver:self forKeyPath:@\"selectedViewController\" options:NSKeyValueObservingOptionNew context:NULL];
// further initialization ...
}
// This method detects if user taps on one of the tabs and removes our \"Home\" view controller from the screen.
- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if (!mAllowSelectTab)
{
[mHomeViewController.view removeFromSuperview];
mAllowSelectTab = YES;
}
return YES;
}
// Here we detect if UITabBarController wants to select one of the tabs and set back to unselected.
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (!mAllowSelectTab)
{
if (object == tabBarController && [keyPath isEqualToString:@\"selectedViewController\"])
{
NSNumber *changeKind = [change objectForKey:NSKeyValueChangeKindKey];
if ([changeKind intValue] == NSKeyValueChangeSetting)
{
NSObject *newValue = [change objectForKey:NSKeyValueChangeNewKey];
if ([newValue class] != [NSNull class])
{
tabBarController.selectedViewController = nil;
}
}
}
}
}
他添加了以下注释:
标签栏的第一个视图控制器
仍然会被加载(尽管对于
很短的时间),因此其viewDidLoad
并且viewWillAppear将被调用
启动后。您可能要添加
一些逻辑,以防止一些
您可能会在其中进行初始化
这些功能直到“真实”显示
用户的控制权
点击(例如使用全局
变量或NSNotificationCenter)。
编辑:这是为了适应Apple-UITabbar。您也可以创建自定义UITabbar。