我想在响应远程通知操作时显示NewMapViewController
,但是如果直接实例化NewMapViewController
,则会松开导航栏。如果我改为实例化navigationController并执行segue,则我看到所有视图控制器(启动和登录)一个接一个地显示,这是非常不愉快的。有没有一种方法可以将导航栏保持在NewMapViewController
中,并避免级联显示所有视图控制器?和往常一样,非常感谢您的帮助。
这是我尝试的:
case Id.checkActionIdentifier:
print("Check tapped")
let userInfo = response.notification.request.content.userInfo
if let routeToCheck = userInfo[NSLocalizedString("Route", comment: "")] as? String {
RoutesArray.routeName = routeToCheck
// print("rotta is: \(routeToCheck)")
}
// let content = response.notification.request.content
// print(" Body \(content.body)")
// print(" Title \(content.title)")
NewMapViewController.routeCheckCounter = 1
// Goes to NewMapViewController but showing the splash and login VC first
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController : UINavigationController = storyboard.instantiateViewController(withIdentifier: "MainNavigationController") as! UINavigationController
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
let vc = initialViewController.viewControllers[0]
vc.performSegue(withIdentifier: "alertToMapSegue", sender: nil)
// goes straight to NewMapViewController but looses nav bar
// let storyboard = UIStoryboard(name: "Main", bundle: nil)
// let initialViewController : UIViewController = storyboard.instantiateViewController(withIdentifier: "NewMapViewController") as! NewMapViewController
// self.window?.rootViewController = initialViewController
// self.window?.makeKeyAndVisible()
更新:
我已经能够避免通过登录vc,但是仍然在
NewMapViewController
之前显示启动画面并显示以下代码:let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "NewMapViewController") as UIViewController
self.window?.rootViewController?.show(vc, sender: self)
最佳答案
看来window
的rootViewController
已经是UINavitationController
了,所以您只需按以下方法抓取并按下NewMapViewController
。
case Id.checkActionIdentifier:
let userInfo = response.notification.request.content.userInfo
if let routeToCheck = userInfo[NSLocalizedString("Route", comment: "")] as? String {
RoutesArray.routeName = routeToCheck
}
NewMapViewController.routeCheckCounter = 1
if let navigationVC = self.window?.rootViewController as? UINavigationController {
let main = UIStoryboard(name: "Main", bundle: nil)
let newMapVC = main.instantiateViewController(withIdentifier: "NewMapViewController") as! NewMapViewController
navigationVC.pushViewController(newMapVC, animated: true)
}