在 Swift 中没有导航控制器的情况下在 viewDidLoad() 中执行 segue [英] perform segue in viewDidLoad() without navigation controller in Swift

查看:35
本文介绍了在 Swift 中没有导航控制器的情况下在 viewDidLoad() 中执行 segue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个视图(视图 1 检查服务器连接,视图 2 显示主要内容,视图 3 显示支持页面)并且我在故事板中创建了它们而没有编码.在启动我的 iOS 应用程序时,视图 1 在检查服务器连接时显示一个微调器.如果连接检查通过,那么我想转到视图 2,如果它失败,那么我想转到视图 3.视图 1 仅用于连接检查,我不想回到此视图.所以,我想我不需要导航控制器,或者?

I have three views (view 1 checks a server connection, view 2 shows the main content, view3 shows the support page) and I created them in the storyboard without coding. On starting my iOS app the view 1 shows a spinner while checking the server connection. If the connection check is passed then I want to go to view 2 and if it's failed then I want to go to view 3. The view 1 is only for the connection check and I don't want to go back to this view. So, I think I don't need a navigation controller, or?

在故事板中,我将所有视图与序列连接起来.在视图 1 的视图控制器中,我这样做:

In storyboard I connected all view with seques. In my view controller of view 1 I do this:

override func viewDidLoad() {
    super.viewDidLoad()

    let result:Bool = server.isServerAvailable(myURL)

    if (result == true) {
        performSegueWithIdentifier("ConnectionCheckToMain", sender: self)
    }
    else {
        performSegueWithIdentifier("ConnectionCheckToSupport", sender: self)
    }
}

但是 viewDidLoad() 函数中的这个 segue 不起作用,但我不知道为什么.我在视图中添加了一个按钮来检查它.我已经实现了与 viewDidLoad() 中相同的代码,它工作正常.单击按钮后,下一个视图将加载.

But this segue in the viewDidLoad() function doesn't work, but I don't know why. I've added a button on view to check it. I've implemented the same code like in viewDidLoad() and it works fine. On clicking the button the next view loads.

知道为什么代码不起作用吗?

Is there any idea why the code doesn't work?

推荐答案

您可以使用此代码进行导航.

You can use this code for navigation.

    let vc : UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ConnectionCheckToMain") as! UIViewController;
    self.presentViewController(vc, animated: true, completion: nil)

这篇关于在 Swift 中没有导航控制器的情况下在 viewDidLoad() 中执行 segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆