简体   繁体   English

Swift go 返回根视图 controller

[英]Swift go back to root view controller

I am new to swift but I have implemented FCM but I am having an issue.我是 swift 的新手,但我已经实施了 FCM,但我遇到了问题。 When I click the notification it loads the NotificationsViewController with the following code.当我单击通知时,它会使用以下代码加载NotificationsViewController

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

Once the NotificationsViewController is loaded I use the following code in on the back button to reassign the root view back to the normal ViewController.加载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

Once I am on the splashScreen it should show a quick image and then move on to the main dashboard using this code.一旦我在splashScreen上它应该显示一个快速图像,然后使用此代码移动到主仪表板。

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

The problem I am having is the splashScreen code is not working when the app loads it from NotificationsViewController , it just hangs on that VC, but if I load the splashScreen without reassigning the root view it works.我遇到的问题是当应用程序从NotificationsViewController加载splashScreen代码时它不起作用,它只是挂在该 VC 上,但是如果我加载splashScreen而不重新分配根视图它可以工作。

So is there some other way I should be doing this?那么还有其他方法我应该这样做吗? I just want to take the user to the NotificationsViewController when they click the notification and then back to the main part of the app when they click a back button.我只想在用户单击通知时将其带到NotificationsViewController ,然后在单击后退按钮时将其返回到应用程序的主要部分。

If you just need to show the NotificationViewController , you just present it in fullscreen.如果您只需要显示NotificationViewController ,则只需全屏显示即可。 You can set it by vc.modalPresentationStyle =.fullScreen .您可以通过vc.modalPresentationStyle =.fullScreen进行设置。 Then to go back to the main app, you just need to dismiss it.然后到 go 回到主应用程序,你只需要关闭它。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM