简体   繁体   中英

Routing to a specific view controller from the app delegate. Swift

I am using Firebase dynamic links and followed along with firebases online videos. After handling the incoming URL I want to route the user to a specific page on the app. My application structure looks like this.

UITabBarController as the rootViewController with 3 tabs -> UINavigationController -> UIViewController ( All done in storyboards )

as seen:

在此输入图像描述

When I get a specific URL, I want to then go to the first tab with some data, and then from there, send the user to the correct page. This makes sure that the user can still travel back through the navigation.

I have tried to achieve this via this:

var storyBoard = UIStoryboard()
    storyBoard = UIStoryboard(name: "Main", bundle: nil)

    let tabViewController = storyBoard.instantiateViewController(withIdentifier: "tabBar") as! TabBar

    let homeVC = storyBoard.instantiateViewController(withIdentifier: "homePage") as! NewHomeViewController
    homeVC.navigatorItem = DeepLinkItem(listId: mediaID, itemId: itemID)

    let appdelegate = UIApplication.shared.delegate as! AppDelegate
    appdelegate.window!.rootViewController = tabViewController

With the line homeVC.navigatorItem = DeepLinkItem(listId: mediaID, itemId: itemID) I can see that the data has been set through because of all my print statements from the other view controllers the are being segued to, however I am still stuck on the homePage (the first view on the tab bar) and the user is not directed to the correct page.

Does someone know how to route to another view controller from the app delegate while still keeping the tab bar as the root. Thank you.

This is what I am after:

在此输入图像描述

So from the appDelegate, I want to go from the tab bar to the navigation controller, then to the first view controller all the way to final view controller, making sure that I keep the navigation path. What I do is pass the data to the first view controller then segue through each. But this isn't working. I am thinking it is due to the app delegate where I am setting the root view controller. Thank you.

EDIT

I have found this answer to a post similar to mine.

My code now looks like this:

    let mainStoryBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let tabBarController = mainStoryBoard.instantiateViewController(withIdentifier: "tabBar") as! UITabBarController
    let homeNavigationController = tabBarController.viewControllers?.first as! UINavigationController
    let homeView = mainStoryBoard.instantiateViewController(withIdentifier: "homePage") as! NewHomeViewController

    homeView.navigatorItem = DeepLinkItem(listId: mediaID, itemId: itemID)

    homeNavigationController.pushViewController(homeView, animated: false)

    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = tabBarController
    self.window?.makeKeyAndVisible()

At the moment it doesn't work, just a blank white screen with the tab bar. However, changing the root view controller to homeNavigationController works, its just the tab bar is no longer there.

If I'm understanding you correctly, you want to START your app in a state where it's at the target view controller. This answer does not apply if the app is already running.

First of all, you need to instantiate the tab bar controller. That will return the entire hierarchy you've constructed in the storyboard file. The viewControllers property of your UITabBarController will contain the three UINavigationController you have set up for your different tabs. The first navigation controller is the one associated with your home tab. You can then manipulate the viewControllers property of the UINavigationController to change the initial stack of view controllers. UINavigationController will always show the last one, so in this example, thirdVc is the one shown in the home tab.

func setupAppFromDeeplink() {
    let tabBarController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "tabBar") as! UITabBarController
    let homeNavigationController = tabBarController.viewControllers?.first as! UINavigationController

    let firstVc = UIViewController() 
    let secondVc = UIViewController()
    let thirdVc = UIViewController()

    homeNavigationController.viewControllers = [firstVc, secondVc, thirdVc]

    window!.rootViewController = tabBarController
}

You will not be able to pass data trough segues with this approach, nor should you. Assign the data to be shown in the view controller when setting up firstVc etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM