UITabBarController - how to make "no tabs" selected at startup?

Is there a way on the iPhone to deselect all UITabBarController tabs? those. my application has a "home page" that does not belong to any tabs in the tab below. Now, when the user touches any tab in the tab, I would like to download the corresponding tab. Is it possible? I have already tried:

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

but it doesn’t help. Any other solutions? You are welcome,

+3
source share
2 answers

I managed to accomplish this with KVO tricks .

: , UITabBarController selectedViewController .

:

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

+7

. - .

, - , popover (.. ), .

-1

Source: https://habr.com/ru/post/1769944/


All Articles