简体   繁体   中英

Swift go back to root view controller

I am new to swift but I have implemented FCM but I am having an issue. When I click the notification it loads the NotificationsViewController with the following code.

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.

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.

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.

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.

If you just need to show the NotificationViewController , you just present it in fullscreen. You can set it by vc.modalPresentationStyle =.fullScreen . Then to go back to the main app, you just need to dismiss it. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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