当用户按下按钮时,如何在我的 swift 应用程序中切换到另一个 viewController?

How can I switch to another viewController in my swift app when user presses the button?

我正在根据 google 个帐户对我的应用进行授权。我有一个包含以下代码的设置视图:

import UIKit
import Google

class Settings: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let background = CAGradientLayer().greenBlue()
        background.frame = self.view.bounds
        self.view.layer.insertSublayer(background, atIndex: 0)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func didTapSignOut(sender: AnyObject) {

        GIDSignIn.sharedInstance().signOut()

    }

}

在图形用户界面中我有一个按钮。当用户单击它时,会话将被此调用破坏:GIDSignIn.sharedInstance().signOut()。但是,到目前为止,没有其他事情发生,用户仍在设置菜单中。我想将他转移到我的 ViewController,其中包含登录表单。

我尝试添加以下代码:

let sb = UIStoryboard(name: "Main", bundle: nil)
if let tabBarVC = sb.instantiateViewControllerWithIdentifier("TabController") as? TabController {
window!.rootViewController = tabBarVC
}

window 未解决。我该怎么做?

在故事板中创建一个从 "Settings" ViewController 到 "TabController" 的转场并将其标识符设置为 "settingsToTabController"。然后在didTapSignOut函数中使用代码self.performSegueWithIdentifier("settingsToTabController", sender: self)。我对此没有太多经验,所以这可能不是最好的方法,但对我来说效果很好。

如果您想按照使用 window 变量发布的原始方式进行操作,您需要从您的委托中获取该 window 实例的实例。您可以通过执行 let window = UIApplication.sharedApplication().delegate?.window! 然后将 UIStoryboard 设置为它来获取它,就像您在上面的代码中所做的那样。现在您的 window 对象将不会被解析。