![](/img/trans.png)
[英]Present viewController over rootViewController when a notification is tapped
[英]Present a ViewController when notification is taped
當用戶點擊本地通知時,我試圖顯示一個詳細視圖控制器。 到目前為止,我已經做到了:
//AppDelegate.swift
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if let id = userInfo["item_id"] as? Int {
if let item = ItemsRepository.shared.getItem(id: id) {
let vc = DetailTableViewController()
vc.item = item
let tabController = self.window?.rootViewController as! UITabBarController
let navigationController = tabController.selectedViewController as! UINavigationController
navigationController.pushViewController(vc, animated: true)
}
}
completionHandler()
}
由於某些原因,此代碼生成異常:
libc++abi.dylib: terminating with uncaught exception of type NSException
`
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if let id = userInfo["item_id"] as? Int {
if let item = ItemsRepository.shared.getItem(id: id) {
let vc = DetailTableViewController()
vc.item = item
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "DetailView") as! DetailTableViewController
vc.item = item
window?.rootViewController = vc
self.window?.makeKeyAndVisible()
}
}
completionHandler()
}
可以,但是標簽欄和導航控制器不再可見!
我在這里做錯了什么?
而不是let vc = DetailTableViewController()
嘗試這個
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "someViewController")
告訴我這是否有效
嘗試這個
let tabController = self.window?.rootViewController as! UITabBarController
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "DetailView") as! DetailTableViewController
vc.item = item
window?.rootViewController = tabController
tabController.present(vc, animated: true, completion: nil)
我已經按照我的故事板文件上的屏幕顯示流程解決了我的問題。 基本上,我已經實例化了Main.storyboard
,然后實例化了隨后的每個視圖控制器。
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if let id = userInfo["item_id"] as? Int {
if let item = ItemsRepository.shared.getItem(id: id) {
let dv = DetailTableViewController()
dv.item = item
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabbar = storyboard.instantiateViewController(withIdentifier: "homeFeed") as! UITabBarController
tabbar.selectedIndex = 0
let nav = tabbar.selectedViewController as! UINavigationController
let main = nav.topViewController as! HomeCollectionViewController
main.performSegue(withIdentifier: "ShowDetailSegue", sender: item)
self.window?.rootViewController = tabbar
window?.makeKeyAndVisible()
}
}
completionHandler()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.