我是 IOS 开发的新手,来自 Android 背景。我正在寻找一种在用户注销后返回到原始 View Controller 的方法。
此帖How can I go back to the initial view controller in Swift?建议我应该使用 unwind segue。
但是,因为我的应用程序的用户 session 可能随时(以及在任何 View Controller 期间)过期。这意味着我需要从每个 View Controller 到 Root View Controller 进行倒带转场!此外,这也会导致一些重复的代码,因为每个 View Controller 都需要一种引用其自己唯一的 segue ID 的方法。
本来,我希望继承UIViewController
并制作一个CustomViewController
我所有的其他ProfileViewController, LoginViewController
然后将子类化。
CustomViewController
将提供辅助方法,例如 func logOut()
.有没有办法做到这一点,也许是通过一些通用的展开转场?
最佳答案
你不能有通用的 unwind segue。您不能继承 ViewController 的 Storyboard属性,包括与 ViewController
关联的 segue。但是您不需要通用的 segue 来解决您的问题。
您可以拥有一个 BaseViewController
,所有其他 View Controller 都将从 BaseViewController
扩展。您可以添加您的逻辑来验证用户是否仍在您的 BaseViewController
中登录(我无法为此编写代码,因为我没有看到您的代码库)。
如果用户不再登录那么你可以简单地调用
class BaseViewController : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//implement your logic to monitor if user is still logged in if user not logged in simply call
self.logOut()
}
func logOut() {
//each child view controller will override and handle log out condition specifically for that ViewController
}
}
最后,要加载初始 ViewController,您可以简单地说在其顶部弹出所有 viewController。假设您的 initialViewController
是您应用的 rootViewController
,您只需将 BaseViewController 中的逻辑修改为
override func viewDidLoad() {
super.viewDidLoad()
//implement your logic to monitor if user is still logged in if user not logged in simply call
self.logOut()
UIApplication.shared.keyWindow?.rootViewController?.navigationController?.popToRootViewController(animated: true)
}
方法的问题:
根据您的问题描述,我相信您的初始 View Controller 保存在 ViewController 堆栈中,您只需在注销时放松到初始 ViewController。
虽然这种方法有效,但它存在三个问题
初始 ViewController 不必要地保存在内存中,直到用户注销后它才会重新出现,因此您实际上可以在用户登录后删除它,并在用户注销后将其恢复。
<如果用户登录并退出应用程序并重新启动应用程序,应用程序将加载初始 View Controller ,但如果用户已经登录,为什么还要再次向他显示初始 View Controller ?
因为您将其他 VC 推到初始 ViewController 之上,这也意味着用户可以简单地继续点击后退按钮并返回到初始 viewController,这没有意义,因为用户点击后退按钮怎么会有与用户登录状态有什么关系??不是吗??
那么更好的解决方案是什么?
在应用启动时更改应用的 Root View Controller 。
打开你的 AppDelegate 和
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//check if user is logged in
guard let window = UIApplication.shared.keyWindow else {
return
}
if (userIsLoggedIn) {
//load view controller that you wanna show after login lets say Home
let homeVC = //instantiate view controller from story board
UIView.transition(with: window, duration: 0.3, options: .transitionCrossDissolve, animations: {
window.rootViewController = homeVC
}, completion: { completed in
// maybe do something here
})
}
else {
//load initial controller
let initialVC = //instantiate view controller from story board
UIView.transition(with: window, duration: 0.3, options: .transitionCrossDissolve, animations: {
window.rootViewController = initialVC
}, completion: { completed in
// maybe do something here
})
}
}
最后,如果用户没有登录并且你最终加载了初始 VC,一旦用户登录了你的初始 VC,你可以使用我上面写的代码来更改根 VC
let initialVC = //instantiate view controller from story board
UIView.transition(with: window, duration: 0.3, options: .transitionCrossDissolve, animations: {
window.rootViewController = initialVC
}, completion: { completed in
// maybe do something here
})
关于ios - 是否可以在 Storyboard 中定义 "generic"unwind segue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48903702/