开发者

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

开发者 https://www.devze.com 2023-01-20 02:49 出处:网络
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

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 ?


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


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.


You can add a UITabBar to your view controller and manage the buttons manually from there, via UITabBarDelegate. Your view controller would be the "base" view, like a homepage.

This is relatively easy to do, unless you want the special "More..." view controller that UITabBarController provides, which you'd have to implement yourself.

0

精彩评论

暂无评论...
验证码 换一张
取 消