【问题标题】:unhighlight uitabbaritem in uitabbarcontroller在 uitabbarcontroller 中取消突出显示 uitabbaritem
【发布时间】:2011-08-04 14:26:03
【问题描述】:

我的 UITabBarController 有 5 个标签栏按钮。在某些活动中,我想取消突出显示所有标签蝙蝠项目。

有人可以帮忙吗?

谢谢,

安基塔

【问题讨论】:

    标签: iphone uitabbarcontroller uitabbar tabbar


    【解决方案1】:

    首先,我想说取消选择所有 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。

    【讨论】:

    • @Joetjah:我不想禁用该按钮。我只是想取消突出显示它。
    • @Ankita Chordia:我实际上发现了这个:stackoverflow.com/questions/3467568/… 如那里的答案所述,我建议使用 UISegmentedControl。这样,您可以将segmentedControl.selectedSegmentIndex 设置为任何值。
    • @Ankita Chordia:实际上,我正在编辑我的答案来回答你的问题。我发现了一些东西......
    • @Joetjah:感谢您的回答,但如果我设置此 tabController.selectedViewController = nil;没有选择标签,但我只想取消突出显示标签
    • @Ankita Chordia:在那种情况下,我不明白区别吗?取消突出显示和取消选择有什么区别?
    猜你喜欢
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    相关资源
    最近更新 更多