【发布时间】:2011-08-04 14:26:03
【问题描述】:
我的 UITabBarController 有 5 个标签栏按钮。在某些活动中,我想取消突出显示所有标签蝙蝠项目。
有人可以帮忙吗?
谢谢,
安基塔
【问题讨论】:
标签: iphone uitabbarcontroller uitabbar tabbar
我的 UITabBarController 有 5 个标签栏按钮。在某些活动中,我想取消突出显示所有标签蝙蝠项目。
有人可以帮忙吗?
谢谢,
安基塔
【问题讨论】:
标签: iphone uitabbarcontroller uitabbar tabbar
首先,我想说取消选择所有 tabbaritems 是一种糟糕的用户体验。它不会被应用商店接受的可能性很高。
说完,我找到了答案here。您可以接受此答案(如果有效!!!),但应向该用户提供道具。他在Key Value Observing中使用了一个技巧,并使用了以下代码:
- (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。
【讨论】:
segmentedControl.selectedSegmentIndex 设置为任何值。