UITabBarController-如何制作“无选项卡"在启动时选择? [英] UITabBarController - how to make "no tabs" selected at start up?

查看:42
本文介绍了UITabBarController-如何制作“无选项卡"在启动时选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iPhone中是否可以取消选择UITabBarController的所有选项卡?即,我的应用程序有一个主页",它不属于下面显示的标签栏上的任何标签.现在,当用户触摸标签栏上的任何标签时,我想加载相应的标签.这可能吗 ?我已经尝试过:

Is there any way in iPhone to unselect all tabs of a UITabBarController ? ie, my application has a "homepage" which does not belong to any tabs on the below displayed tabbar. Now when user touches any tab on the tabbar, I would like to load the corresponding tab. Is this possible ? I have already tried:

self.tabBarController.tabBarItem.enabled =否;self.tabBarController.selectedIndex = -1;

self.tabBarController.tabBarItem.enabled = NO; self.tabBarController.selectedIndex = -1;

但这无济于事.还有其他解决方案吗?请吗?

but this does not help. Any other solutions ? Please ?

推荐答案

我已经成功地使用

I've managed to accomplish this using KVO tricks.

这个想法很简单:我们在UITabBarController尝试设置其selectedViewController属性并将其立即设置回nil时进行跟踪.

The idea is simple: we track down when UITabBarController tries to set its selectedViewController property and immediately set it back to nil.

示例代码:

- (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)导致该控制器真正"显示为止.

However, one small note: the first view controller from tabbar still will be loaded (although for a very short time), so its viewDidLoad and viewWillAppear will be called after startup. You may want to add some logic to prevent some initializations you probably may do in these functions until "real" display of that controller as a result of user tap (using for example global variables or NSNotificationCenter).

这篇关于UITabBarController-如何制作“无选项卡"在启动时选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆