rony
rony

Reputation: 520

Access UIViewController from UITabBarController and UINavigationController

I am developing an application based on UITabbar and the view hierarchy as follows.

UITabBarController ----> UINavigationController ----> UIViewController

I have push notification payload which will open specific UIViewController, i can explicitly open UIViewController directly using view controller Storyboard ID, but tabBar and Navbar won't show. How can I go to specific View Controller and show TabBar and NavController from AppDelegate didReceiveRemoteNotifications.

Thanks!

Upvotes: 1

Views: 1040

Answers (3)

Robert Jones
Robert Jones

Reputation: 1

Follow the hierarchy from App Delegate programmatically. In case your entry point is from the Storyboard, setup a UIWindow in your AppDelegate so you could set the UITabBarController as the following.

//self.tabBarController is you TabBar from Storyboard, or programatically initialized
self.window.rootViewController = self.tabBarController; 

Then whenever you have a notification in didReceiveRemoteNotifications sort the notification out by type, and find the view controller:

//Let's say the View Controller being accessed is in the first position of the stack of viewcontroller from UITabBarController & UINavigationController 
UINavigationController *navViewController = self.tabBarController.viewControllers.firstObject;
UIViewController *accessedViewController = navViewController.viewcontroller.firstObject;

Upvotes: 0

Federico Malagoni
Federico Malagoni

Reputation: 792

You have to instantiate all of your VC and set all of them as root of his predecessor :

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let yourVC = mainStoryboard.instantiateViewControllerWithIdentifier("YourVC_Identifier");
    let yourNavController = mainStoryboard.instantiateViewControllerWithIdentifier("YourNAV_Identifier") as! UINavigationController
    let yourTabController = mainStoryboard.instantiateViewControllerWithIdentifier("YourTAB_Identifier") as! UITabBarController

    yourNavController.setViewControllers([yourVC], animated: false)
    yourTabController.setViewControllers([yourNavController], animated: false)


    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    self.window?.rootViewController = yourTabController
    self.window?.makeKeyAndVisible()



    return true
}

Upvotes: 1

Joe Daniels
Joe Daniels

Reputation: 1706

[[UIApplication sharedApplication] keyWindow] has a property .rootViewController. Presumably thats your tab bar. On this controller, you can set the active tab and switch out view controllers with the .viewControllers property. Now assuming one of these is your UINavigationController that should also have a property .rootViewController. Instantiate from the storyboard and either set the root or push the view controller on top of the navigation controller.

Upvotes: 0

Related Questions