如何从AppDelegate实例化多个视图控制器?

时间:2017-04-04 19:02:51

标签: ios swift uiviewcontroller initialization appdelegate

Storyboard

有一个容器视图,它包含3个视图控制器(V1,V2和V3)。我可以通过向左或向右滑动来切换A,B或C. A或B都包含自己的集合视图。如果我点击A或B内的集合视图中的任何单元格,PlayerVC(在上面的图片中命名为Player ^^)将启动并使用AVPlayer开始播放视频。

问题是:由于我使用通用链接,用户直接进入playerView播放视频,但当他们按下完成按钮时,应用程序崩溃。我相信的问题是因为视图控制器上的其余部分未初始化?如何初始化容器视图和其他视图控制器?或者,如果这不是问题,请告诉我这是什么问题。

这是带有一些示例代码的app委托。如果可能的话请提供代码帮助!

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
    }
}

编辑 - 我在PagingViewController中实例化3个VC的附加代码(容器视图)

  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)
......

1 个答案:

答案 0 :(得分:0)

如果您正在处理UIPageViewController,要设置正确的孩子UIViewController,您可以:

// 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)
    }
}

然后在AppDelegate的{​​{1}}:

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