首页 > 解决方案 > UIPageViewController subviewcontroller 无法在 viewDidAppear 上执行 segue

问题描述

我有一个包含两个子视图控制器的 UIPageController。开始时,当用户未获得授权时,我试图执行从 SubViewControllerOne 到 LoginViewController 的 segue。我在 viewDidAppear 中有一个 segue,如下所示:

performSegue(withIdentifier: "authorizeSegue", sender: self);

这会产生不成功的 segue 和警告

Warning: Attempt to present <Volley.LoginViewController: 0x7f98bd801e00> on <Volley.SubViewControllerOne: 0x7f98bd608400> whose view is not in the window hierarchy!

我过去曾成功使用 ViewControllers 和 TabBarViewControllers 完成此操作。

当我将 segue 包装在计时器中或通过 IBAction 触发时,segue 工作得非常好。我相信它与 PageViewController 有关。

class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate {

    var subViewControllers = [UIViewController]()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
        self.dataSource = self
        self.edgesForExtendedLayout = [];
        // Do any additional setup after loading the view.

        let subViewControllerOne = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SubViewControllerOne") as! SubViewControllerOne
        let subViewControllerTwo = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SubViewControllerTwo") as! SubViewControllerTwo
        subViewControllers = [yourPodcastViewController, appearancesViewController]
        setViewControllers([subViewControllers[0]], direction: .forward, animated: true, completion: nil)
    }

    required init?(coder: NSCoder) {
        super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
    }

导航控制器和 PageViewController 在此处输入图像描述

从 SubViewControllerOne 到 LoginViewController 的转场 在此处输入图像描述

标签: iosswiftxcodesegue

解决方案


Welp,我之前在 tableviews 上通过 didSelect 运行 segues 时遇到过这个问题。解决方案是将其包装在主线程中。不知道为什么这是必要的,但它解决了我的问题。

DispatchQueue.main.async {
  self.performSegue(withIdentifier: "authorizeSegue", sender: self);
}

推荐阅读