【问题标题】:How can I switch to another viewController in my swift app when user presses the button?当用户按下按钮时,如何在我的 swift 应用程序中切换到另一个 viewController?
【发布时间】:2016-02-13 16:52:44
【问题描述】:

我正在根据 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()

    }

}

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

我尝试添加以下代码:

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

但是window 未解决。我该怎么做?

【问题讨论】:

  • 请发布您的故事板结构,以便我为您提供帮助。
  • 查看我的详细回复,了解为什么 window 对象无法解析。

标签: ios swift google-signin


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-13
      • 2019-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多