Monday, June 10, 2013

UITabBarController selectedViewController example in Objective C (iOS).


UITabBarController selectedViewController

The view controller associated with the currently selected tab item.

@property(nonatomic, assign) UIViewController *selectedViewController

Discussion of [UITabBarController selectedViewController]
This view controller is the one whose custom view is currently displayed by the tab bar interface. The specified view controller must be in the viewControllers array. Assigning a new view controller to this property changes the currently displayed view and also selects an appropriate tab in the tab bar. Changing the view controller also updates the selectedIndex property accordingly. The default value of this property is nil.[UITabBarController selectedViewController]

In iOS 3.0 and later, you can use this property to select any of the view controllers in the viewControllers property. This includes view controllers that are managed by the More navigation controller and whose tab bar items are not visible in the tab bar. You can also use it to select the More navigation controller itself, which is available from the moreNavigationController property. Prior to iOS 3.0, you could select only the More navigation controller and the subset of view controllers whose tab bar item was visible. Attempting to set this property to a view controller whose tab bar item was not visible had no effect.

UITabBarController selectedViewController example.
if ([[WSFUserDefaults sharedInstance] savedTabBarLocation] > 0) {

     if ([[WSFUserDefaults sharedInstance] savedTabBarLocation] > 3) {
     UIViewController *selectViewController = [tabBarController.viewControllers objectAtIndex:[[WSFUserDefaults sharedInstance] savedTabBarLocation]];
     [tabBarController setSelectedViewController:tabBarController.moreNavigationController];
     [tabBarController.moreNavigationController popToRootViewControllerAnimated:NO];//make sure we're at the top level More
     [tabBarController.moreNavigationController pushViewController:selectViewController animated:NO];
     }
     else {
     [tabBarController setSelectedIndex:[[WSFUserDefaults sharedInstance] savedTabBarLocation]];
     }
     }

Example of [UITabBarController selectedViewController].
        DefaultView *defaultView = [[DefaultView alloc]initWithNibName:@"DefaultView" bundle:[NSBundle mainBundle]];
        [self.tabBarController setSelectedViewController:nil];
        [self.tabBarController setSelectedViewController:defaultView];

UITabBarController selectedViewController example.
- (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;
                }
            }
        }
    }
}

End of UITabBarController selectedViewController example article.