AppDelegate - 如何推送我的viewcontroller而不是present,原因是我想在我的下一个viewcontroller上也有标签栏和导航栏。我试过这个:“controller.navigationController?.pushViewController(controller, animated: true)” 但它给了我 fatal error ,在展开可选值时发现 nil。有什么想法吗?
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
if Defaults.hasKey(.logged), let logged = Defaults[.logged], logged == true {
let userInfo = notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
let topWindow = UIWindow(frame: UIScreen.main.bounds)
topWindow.rootViewController = UIViewController()
topWindow.windowLevel = UIWindowLevelAlert + 1
let alert = UIAlertController(title: userInfo["title"] as? String, message: userInfo["body"] as? String, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("Show Product", comment: "confirm"), style: .default, handler: {(_ action: UIAlertAction) -> Void in
if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ProductDetailsViewController") as? ProductDetailsViewController {
controller.productFromNotificationByCode(code: userInfo["product_code"] as! String, productID: userInfo["product_id"] as! String)
if let window = self.window, let rootViewController = window.rootViewController {
var currentController = rootViewController
while let presentedController = currentController.presentedViewController {
currentController = presentedController
}
currentController.present(controller, animated: true, completion: nil)
}
}
}))
alert.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: "cancel"), style: .destructive, handler: {(_ action: UIAlertAction) -> Void in
topWindow.isHidden = true
}))
topWindow.makeKeyAndVisible()
topWindow.rootViewController?.present(alert, animated: true, completion: { _ in })
}
}
completionHandler([])
}
最佳答案
首先,您应该尽可能保持您的 AppDelegate 干净。应用委托(delegate)实际上应该只处理应用启动时发生的事情。通常情况下,这只是设置您的初始窗口(即进入您的应用程序的入口)。所以你应该想出你的第一个 View Controller 并将其设置为你的 rootViewController 和 AppDelegate 中 FinishedLaunching 中的窗口。如果你想使用 UINavigationController(听起来像你做的),你可以在 UINavigationController (UINavigationController(FirstViewController())
) 中嵌入你想要开始的 ViewController,并将导航 Controller 设置为 rootViewController。
听起来您还没有完成将您正在使用的 View Controller 嵌入到 UINavigationController 中的步骤。完成此步骤后,您应该可以使用 show(nextViewController)
这是您要从中“推送”的 UIViewController 上的一个函数。如果您在 UINavigationController 中,这将执行推送动画,并且下一个 View 将成为导航堆栈的一部分,就像您想要的那样。
关于ios - AppDelegate PUSH View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44168630/