我想创建一个页面,如果你按下“登录”按钮(女巫位于NavigationBar,左侧项目),之后显示第二页(又名主页)的用户可以退出。同时我想记住用户使用UserDefault登录。
这是我的AppDelegate文件:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
theViewController(controllerIs: LoginPage())
return true
}
fileprivate func theViewController(controllerIs: UIViewController) {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = UINavigationController(rootViewController: controllerIs)
}
}
登录视图:
import UIKit
class LoginPage: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .green
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Login", style: .plain, target: self, action: #selector(handleLogin))
}
@objc private func handleLogin() {
let root = UIApplication.shared.keyWindow?.rootViewController
root?.present(SignOutPage(), animated: true, completion: {
//some code here
})
}
}
退出视图:
import UIKit
class SignOutPage: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .yellow
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Sign Out", style: .plain, target: self, action: #selector(handleSignOut))
}
@objc private func handleSignOut() {
let root = UIApplication.shared.keyWindow?.rootViewController
root?.present(LoginPage(), animated: true, completion: {
//some code here
})
}
}
答案 0 :(得分:0)
执行此操作时:
let root = UIApplication.shared.keyWindow?.rootViewController
root?.present(SignOutPage(), animated: true, completion: {
//some code here
})
您将以模态方式呈现SignOutPage视图控制器,并将其置于根视图控制器上。
您想要做的是将其推送到导航控制器堆栈,这样您就需要这样的东西:
navigationController?.pushViewController(SignOutPage(), animated: true)
以同样的方式退出而不是呈现视图控制器时,您希望从堆栈中弹出当前的一个,执行以下操作:
navigationController?.popViewController(animated: true)
这会将应用程序返回到上一个视图控制器(登录控制器)。
答案 1 :(得分:0)
导航层次结构中有两件事情要做。
导航堆栈仅在pushViewController
的情况下才会出现。如果是present
模态,则需要启动新的导航堆栈
let navVC = UINavigationController.init(rootViewController: viewController)
self.present(navVC, animated: true, completion: nil)
其中,viewController是新UIViewController
的对象。
每次使用导航堆栈(用于推送)和视图控制器(用于存在)时,不要使用UIApplication.shared.keyWindow?.rootViewController
:
// Push
self.navigationController?.pushViewController(viewController, animated: true)
// Present Modally
self.present(viewController, animated: true, completion: nil)
其中,self
是您的视图控制器的对象,viewController
是新UIViewController
的对象。
因此,handleLogin()
上的LoginPage
将如下:
// Push
@objc private func handleLogin() {
guard let vc = UIStoryboard.init(name: "Main", bundle: .main).instantiateViewController(withIdentifier: "SignOutVC") as? SignOutPage else { return }
self.navigationController?.pushViewController(vc, animated: true)
}
// Pop
@objc private func handleLogin() {
guard let vc = UIStoryboard.init(name: "Main", bundle: .main).instantiateViewController(withIdentifier: "SignOutVC") as? SignOutPage else { return }
let navVC = UINavigationController.init(rootViewController: vc)
self.present(navVC, animated: true, completion: nil)
}
并且handleSignOut()
上的SignOutPage
将如下:
// Push
@objc private func handleSignOut() {
self.navigationController?.popViewController(animated: true)
}
// Pop
@objc private func handleSignOut() {
self.dismiss(animated: true, completion: nil)
}
注意:您需要将storyboard-identifier设置为: