5

At app launch, I have UITabBarController as the root view controller. For this tab bar, I have total 7 ViewControllers. Inside AppDelegate.swift in method application(application:didFinishLaunchingWithOptions:), if I do following, it works properly:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        self.tabBarController = self.window?.rootViewController as! UITabBarController
        self.tabBarController.selectedIndex = 2

        return true
    } 

This works perfectly and the third item in the tab bar is selected when the application is launched.

But, when I want to select any item which has gone under the more navigation controller (which means any index beyond 3), the self.tabBarController.selectedIndex = 5 simply does not work. The mentioned statement does work at any part after the app launch is complete, i.e., if I do self.tabBarController.selectedIndex = 5 inside the viewDidAppear of first ViewController, it works; but it doesn't work at the time of app launch,i.e., in application(application:didFinishLaunchingWithOptions:). The tab bar selected item remains to default after the launch happens. What is the way I can change the selected index beyond 3 (which goes under moreNavigationController) at the app launch?

Rohan Sanap
  • 2,773
  • 2
  • 21
  • 39
  • http://stackoverflow.com/a/33375674/2303865 – Leo Dabus Jan 30 '16 at 05:29
  • @Leo Dabus, I want to handle deep link of push notification on application launch. So if my app in not running and push notification is received, I click on push notification. At this point `application(application:didFinishLaunchingWithOptions:)` is triggered, but first view controller is not still loaded so how will my observer work at this point as the view controller instance is not available to observe? – Rohan Sanap Jan 30 '16 at 05:44
  • I tried your suggestion. Unfortunately it is not working :( – Rohan Sanap Jan 30 '16 at 06:21
  • try this http://stackoverflow.com/a/35080095/2303865 – Leo Dabus Jan 30 '16 at 06:28

2 Answers2

0

try adding

// Override point for customization after application launch.
        self.tabBarController = self.window?.rootViewController as! UITabBarController
        self.tabBarController.selectedIndex = 2
        self.tabBarController.view.layoutIfNeeded()

        return true
David Yang Liu
  • 1,170
  • 2
  • 10
  • 19
0

If you are using storyboard. This answer worked for me you can create an Inspectable var in your tabBarController class and specify the index number in storyboard.

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
Serl
  • 240
  • 1
  • 14