selectedIndex UItabBarController not working

appdelegate, ios, objective-c, uitabbarcontroller

Solution

Because you push to your tab bar controller from a login controller, there's no way to get a reference to the tab bar controller from the app delegate, because it doesn't exist until it's pushed. The way I would fix this is to change the structure of your app. While there's nothing wrong (as in it will cause a crash) with your current structure, I don't like putting login controllers first in line, because they're used once to log in, then it would be better if they would go away. I would make the tab bar controller the window's root view controller, and present the login controller modally, with no animation, from the viewDidAppear method of the controller in the first tab -- this will make the login controller the first thing the user sees. When you're done with login, you just dismiss, and your back to the first tab.

If you use this structure, then you can use my suggestion to set the selected index:

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo{
    NSLog(@"opening from a notification!!");
    self.tabBarController = self.window.rootViewController;
    self.tabBarController.selectedIndex = 3;
    NSLog(@"%i is the tabbar item",tabBarController.selectedIndex);
}

Problem

I know this is gonna be an easy fix, but for some reason when i try to fix my issue with other solutions, it just doesnt work. In my app I set up Push Notifications, and I'm trying to set up the functionality of opening the app to a certain `tabBarItem` when the app is opened from a Notifications. here is the code i have so far, please tell me if I'm even using the right method. I'm not very experienced with `AppDelegate.m` so please forgive me if its completely wrong. AppDelegate.m ``` #import <CoreLocation/CoreLocation.h> @interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate,CLLocationManagerDelegate>{ CLLocationManager *locationManager; } @property (strong, nonatomic) UIWindow *window; @property(nonatomic,readonly) UITabBar *tabBar; @property(nonatomic,strong) UITabBarController *tabBarController; @end ``` AppDelegate.m ``` - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UIApplication* app = [UIApplication sharedApplication]; app.networkActivityIndicatorVisible = YES; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)]; return YES; } - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo{ /* this works just fine*/ NSLog(@"opening from a notification!!"); /*this is what doesnt work at all*/ self.tabBarController = [[UITabBarController alloc] init]; [self.tabBarController ]; self.tabBarController.selectedIndex = 3; /*also when i do nslog the selectedIndex it gives me this value "2147483647"*/ NSLog(@"%i is the tabbar item",tabBarController.selectedIndex); } ```

Original source

Related problems