How do I instantiate multiple View Controllers from an AppDelegate?

Storyboard

There is a container that contains 3 view controllers (V1, V2 and V3). I can switch from A, B or C by swiping left or right. Both A or B contain their own collection view. If I click on any cell in the collection view inside A or B, the PlayerVC player (named Player in the figure above ^^) starts up and the video starts playing with the AVPlayer.

The problem is this: Since I am using universal links, the user goes straight to the playerView to play the video, but when they click Finish, the application crashes. The problem I believe is that the rest on the view controllers are NOT initialized? How do I initialize the container view and other view controllers? Or if that's not a problem, please let me know what the problem is.

Here is an application delegate with some sample code. Please provide code if possible to help!

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    if let pagingViewController = window?.rootViewController as? PagingViewController {
        pagingViewController.videoPlaybackManager = videoPlaybackManager
    }
    return true
}



    class AppDelegate: UIResponder, UIApplicationDelegate {
    // Other App Delegate methods.....

    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {

        // 1) Make sure the passed `user activity` has expected characteristics.
        guard userActivity.activityType == NSUserActivityTypeBrowsingWeb, let url = userActivity.webpageURL else {
            return false
        }

        // HELP: I need to get to `PlayerVC` from here?

        return true

        // If we can't do the above we default to opening the page in safari
    }
}

      

EDIT - Additional code for how I instantiate 3 VCs inside PagingViewController (container view)

  private func setupViewControllers() {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)

        page1 = storyboard.instantiateViewController(withIdentifier: StoryboardIdentifiers.feedViewController.rawValue) as! FeedViewController
        page1.view.translatesAutoresizingMaskIntoConstraints = false
        page1.delegate = self
        scrollView.addSubview(page1.view)
        addChildViewController(page1)
        page1.didMove(toParentViewController: self)
        // Inject dependency.
        page1.videoPlaybackManager = videoPlaybackManager

        page2 = storyboard.instantiateViewController(withIdentifier: StoryboardIdentifiers.favoritesViewController.rawValue) as! FavoritesViewController
        page2.view.translatesAutoresizingMaskIntoConstraints = false
        page2.delegate = self
        scrollView.addSubview(page2.view)
        addChildViewController(page2)
        page2.didMove(toParentViewController: self)

        page3 = storyboard.instantiateViewController(withIdentifier: StoryboardIdentifiers.settingsViewController.rawValue) as! SettingsViewController
        page3.view.translatesAutoresizingMaskIntoConstraints = false
        scrollView.addSubview(page3.view)
        addChildViewController(page3)
        page3.didMove(toParentViewController: self)
......

      

+3


source to share


1 answer


If you are dealing with UIPageViewController

in order to set the correct child UIViewController

, you can:

// In PagingViewController
func loadChildViewControllerFromUniversalLink(myParameter: Bool) {
    // The condition you need to satisfied in order to present
    // the correct child view controller
    if  myParameter { // is satisfied
        let universalLinkVc = MyChildViewController() // load your VC here
        let direction : UIPageViewControllerNavigationDirection = .forward // or .reverse, as you wish
        self.setViewControllers([universalLinkVc], direction: direction, animated: animated, completion: nil)
    }
}

      



And then in AppDelegate

application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool

:

myPagingViewControllerInstance.loadChildViewControllerFromUniversalLink(true)

      

0


source







All Articles