如果我创建一个新窗口并makeKeyAndVisible

时间:2019-04-30 00:39:55

标签: ios swift

我正在尝试注销并返回到Login Viewcontroller。

因此,我创建一个窗口,设置rootViewController,然后设置makeKeyAndVisible。 (不在AppDelegate中)

class AppManager: NSObject {
var window: UIWindow?

func goToLoginPage() {
    window = UIWindow(frame: UIScreen.main.bounds)
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let loginVC = storyboard.instantiateViewController(withIdentifier: 
    "LoginVC") as! LoginVC
    window?.rootViewController = loginVC
    window?.makeKeyAndVisible()
}

}

它可以工作,但是我在想上一个窗口会发生什么。它会自动关闭并释放吗?如果没有,我需要这样做吗?

还是直接使用“ UIApplication.shared.delegate?.window !!”更好?像下面这样,而不是创建一个新窗口?

UIApplication.shared.delegate?.window!!.rootViewController = homeVC

1 个答案:

答案 0 :(得分:2)

您可以从AppDelegate获取现有窗口,然后更改rootViewControlller

if let appDelegate = UIApplication.shared.delegate as? AppDelegate, let window = appDelegate.window {
    window.rootViewController = loginVC
}