Swift 回到根视图控制器

Posted

技术标签:

【中文标题】Swift 回到根视图控制器【英文标题】:Swift go back to root view controller 【发布时间】:2020-05-18 01:14:57 【问题描述】:

我是 swift 新手,但我已经实施了 FCM,但我遇到了问题。当我单击通知时,它会使用以下代码加载NotificationsViewController

let sb = UIStoryboard(name: "Main", bundle: nil)
let otherVC = sb.instantiateViewController(withIdentifier: "NotificationsViewController") as! NotificationsViewController
self.window?.rootViewController = otherVC;

一旦NotificationsViewController 被加载,我在后退按钮上使用以下代码将根视图重新分配回普通 ViewController。

let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let redViewController = mainStoryBoard.instantiateViewController(withIdentifier: "splashScreen") as! ViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = redViewController

一旦我在splashScreen 上,它应该会显示一个快速图像,然后使用此代码转到主仪表板。

let vw = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DashboardViewController") as! DashboardViewController
self.navigationController?.pushViewController(vw, animated: true)

我遇到的问题是当应用程序从NotificationsViewController 加载它时splashScreen 代码不起作用,它只是挂在那个VC 上,但是如果我加载splashScreen 而不重新分配根视图它可以工作。

那么我还有其他方法吗?我只想在用户点击通知时将他们带到NotificationsViewController,然后在他们点击后退按钮时返回应用程序的主要部分。

【问题讨论】:

【参考方案1】:

如果您只需要显示NotificationViewController,您只需全屏显示即可。您可以通过vc.modalPresentationStyle = .fullScreen 进行设置。然后回到主应用程序,你只需要关闭它。 :)

【讨论】:

我尝试使用以下内容,但它没有打开NotificationViewController,当我单击通知下拉菜单时它只是打开了应用程序的主要部分。我还是 swift 的新手,所以也许我之后没有打电话给别的东西? let sb = UIStoryboard(name: "Main", bundle: nil) let otherVC = sb.instantiateViewController(withIdentifier: "NotificationsViewController") as! NotificationsViewController otherVC.modalPresentationStyle = .fullScreen 你必须让 TopViewController(它有导航控制器)来展示那个 VC

以上是关于Swift 回到根视图控制器的主要内容,如果未能解决你的问题,请参考以下文章

根视图控制器 Swift

在 Swift 中以编程方式设置根视图控制器

Swift - WatchKit:如何将数据返回到根视图控制器?

swift 내부에内容视图控制器를过渡효과를주어바꿀수있는根视图控制器

如何在弹出到根视图 Swift 后立即更改选项卡?

如何以编程方式返回具有全局功能的根视图控制器?