是否可以在故事板中定义“通用”展开segue?
我是IOS开发的新手,来自Android背景。 我正在寻找一种方法,一旦用户注销,返回到我的原始视图控制器。
这篇文章如何回到Swift中的初始视图控制器? 建议我使用unwind segue。
但是,因为我的应用程序的用户会话可以随时(以及在任何视图控制器期间)到期。 这意味着我需要从每个视图控制器到根视图控制器进行倒带。 此外,这也会导致一些重复的代码,因为每个视图控制器都需要一种引用其自己唯一的segue ID的方法。
最初,我希望CustomViewController
UIViewController
并创建一个CustomViewController
,然后我的所有其他ProfileViewController, LoginViewController
将ProfileViewController, LoginViewController
。
CustomViewController
将提供辅助方法,例如func logOut()
。 有没有办法做到这一点,也许通过一些通用的展开segue?
你不能通用unwind segue。 您无法inheritanceViewController的storyboard属性,包括与ViewController
关联的segue。 但是你不需要通用的segue来解决你的问题。
您可以拥有一个BaseViewController
,所有其他视图控制器都将从BaseViewController
扩展。 您可以添加逻辑以validation用户是否仍在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) }
问题与方法:
根据您的问题描述,我相信您的初始视图控制器保存在ViewController堆栈中,您只需在注销时放松到初始ViewController。
虽然这种方法有效,但它有三个问题
-
初始ViewController不必要地保留在内存中,直到用户注销后才会再次出现,因此一旦用户登录就可以实际删除它,并在用户注销后将其恢复。
-
如果用户登录并退出应用程序并重新启动应用程序,应用程序将加载初始View控制器,但如果用户已经登录,为什么你甚至会再次向他显示初始视图控制器?
-
因为你正在推动其他VC在最初的ViewController之上,这也意味着用户可以简单地继续按下按钮并回到初始的viewController,这是没有意义的,因为用户点击后退按钮怎么来与用户登录有关在州? 不是吗?
什么是更好的解决方案呢?
在应用启动时更改应用的根视图控制器。
打开你的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 })
如果要返回登录视图控制器,可以从下一个视图设置单个倒带(退出)到该视图。 然后使用通知中心在您的应用中的任何位置发布通知。 “登录旁边”视图观察通知并执行倒带segue。
- 如何在Objective C中列出来自Amazon S3存储桶的特定path中的文件夹?
- Swift如何获得UIAlertController标题的高度
- 使用GPUImage重新创buildiOS 7 Glass效果
- 当我在Google Maps SDK for iOS中点击另一个标记时,是否可以拦截标记?
- 关于viewController的“viewDidLoad”和“viewWillAppear”方法
- 在iOS设备中获取默认用户电子邮件
- 如何在MPMoviePlayerController播放失败时获取错误描述
- 你的故事会是什么? 满足20天的代码:Swift Playgrounds项目,卢旺达,2016年。
- 在WatchKit中相当于prepareForSegue