在没有用户交互的情况下从一个View Controller切换到另一个View

时间:2018-12-01 01:11:56

标签: ios swift

我试图弄清楚如何制作一个代码,该代码将使一个View Controller转到另一个View Controller,而无需按下按钮即可转到第二个View Controller。

LaunchScreen.storyboard进入Main.storyboard几乎相同,而无需按下按钮,它只是自动执行。我是编码新手,非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

这取决于您实际如何显示视图控制器。

如果您尝试复制启动屏幕,则可能不希望将下一个VC推送到堆栈中。

我想您提供的信息的最佳选择是在短暂的延迟后更改rootViewController。这样的事情应该做:

top: 0

答案 1 :(得分:0)

是的,您无需任何操作即可推入或展示另一个视图控制器。您只需要在要触发它的地方调用推送或当前代码即可。

例如: 如果您希望在计时器功能上触发它,请在其中添加您的推送或当前代码。

//I added this code in first viewController's viewDidAppear, so your second controller will be called after 10 seconds.
    override func viewDidAppear(_ animated: Bool) {

    super.viewDidAppear(animated)

    //If you are using Storyboard
    //10.0 seconds
    DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) {

        //If your first viewController is in Navigation stack
        if let secondViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewControllerID") as? SecondViewController {

            if let navigator = self.navigationController {

                navigator.pushViewController(secondViewController, animated: true)
            }
        }

        //If your first viewController is NOT in Navigation stack
        if let secondViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewControllerID") as? SecondViewController {

            self.present(secondViewController, animated: true, completion: nil)
        }
    }
 }

请参阅此帖子:How to push and present to UIViewController programmatically without segue in iOS Swift 3