UITabBarController - how to make “no tabs” selected at start up?

风流意气都作罢 提交于 2019-12-22 09:59:54

问题


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 = NO; self.tabBarController.selectedIndex = -1;

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


回答1:


I've managed to accomplish this using KVO tricks.

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

Example code:

- (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;
                }
            }
        }
    }
}

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).




回答2:


Showing a tab bar with no tab selected would break user experience. A tab bar should always have one element selected and show the appropriate page - everything else is confusing.

So if you need to show something else, you should make it a popover (i.e. a sliding sheet from the bottom) that can be dismissed.



来源:https://stackoverflow.com/questions/3952287/uitabbarcontroller-how-to-make-no-tabs-selected-at-start-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!