iOS Swift 始终以相同的 View Controller 开始

标签 ios swift uinavigationcontroller segue

enter image description here

我面临两个问题;

  1. 如何呈现登录屏幕始终意味着即使应用程序来自后台,我也需要显示登录页面。

  2. 我在 Home Controller 中有一个注销按钮,我使用 Show Segue 从登录状态转到了主页状态,但是我无法在单击注销时返回到登录状态。 我使用下面的代码移动到登录页面

    self.navigationController?.popViewControllerAnimated(true)
    

最佳答案

您的登录页面不在导航 Controller 内。您的导航 Controller 无法弹出。相反,我们需要关闭导航 Controller 本身。

self.navigationController?.dismissViewControllerAnimated(false, completion: nil)

因此,只要您想返回到第一个登录页面,就可以随时调用此代码。当应用程序从后台返回时执行此操作时,点击应用程序委托(delegate)的 applicationWillEnterForeground(application:) 方法。

鉴于我们可能不会从应用程序委托(delegate)中引用导航 Controller 或我们的任何 View Controller ,我们可以改为让应用程序委托(delegate)的窗口的 Root View Controller (应该是您的登录 Controller )关闭所有内容.所以在 applicationWillEnterForeground(application:) 中添加:

self.window?.rootViewController?.dismissViewControllerAnimated(false, completion: nil)

关于iOS Swift 始终以相同的 View Controller 开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31430357/

相关文章:

ios - UINib 实例化的对象没有显示其正确的类?

ios - UITabBar 不改变其背景颜色

swift - 在 SKScene 中隐藏和取消隐藏按钮

ios - 自定义 UINavigationBar 后退按钮

ios - 想要从容器 View 推送到另一个 View Controller

ios - 自定义 UICollectionViewCell 自动布局 NSInternalInconsistencyException 错误

ios - dyld : Library not loaded: @rpath/Alamofire. 框架/Alamofire

swift - 透明 Popover 和黑色父级 UIViewController

ios - Swift 3 Firebase 数据库获取 URLString

ios - UINavigationBar 在推送 View 动画之前消失并在其之后出现。怎么了?