xcode 使用 Swift 在 didReceiveRemoteNotification 中显示特定的视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29910129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Present specific view controller in didReceiveRemoteNotification with Swift
提问by Aaron
When a user taps a push notification, I want them to be taken to a specific table view controller in my app. This table view controller is embedded in a navigation controller, which is embedded in a tab bar controller (my root view controller). The image shown below visualizes this.
当用户点击推送通知时,我希望它们被带到我的应用程序中的特定表视图控制器。这个表视图控制器嵌入在导航控制器中,导航控制器嵌入在标签栏控制器(我的根视图控制器)中。下图显示了这一点。
The root view Tab Bar Controller has a storyboard ID of "HomeVC" and a class name of "HomeViewController", the Navigation Controller has a storyboard ID of "SettingsNavigationVC", and the Table View Controller has a storyboard ID of "SettingsTableVC" and a class name of "SettingsNavigationVC".
根视图标签栏控制器的故事板 ID 为“HomeVC”,类名称为“HomeViewController”,导航控制器的故事板 ID 为“SettingsNavigationVC”,表视图控制器的故事板 ID 为“SettingsTableVC”和一个“SettingsNavigationVC”的类名。
I have push notifications working. By working, I mean I can send a message from a device and receive it on another device, but when the receiver taps the notification I can't seem to get any other view controller to open other than the root view controller. According to the push notification guideI'm using, I'm using the following code in the didReceiveRemoteNotification
method:
我有推送通知工作。通过工作,我的意思是我可以从一个设备发送一条消息并在另一个设备上接收它,但是当接收器点击通知时,我似乎无法打开除根视图控制器之外的任何其他视图控制器。根据我使用的推送通知指南,我在方法中使用了以下代码didReceiveRemoteNotification
:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject: AnyObject]) {
let rootVC = self.window!.rootViewController
if PFUser.currentUser() != nil {
let settingsTableVC = SettingsTableViewController()
rootVC?.navigationController?.pushViewController(settingsTableVC, animated: false)
}
}
What am I doing wrong, or what must I do to present the right view controller?
我做错了什么,或者我必须做什么才能呈现正确的视图控制器?
回答by flizana
Try this
尝试这个
let storyboard = UIStoryboard(name: "YourStoryboardName", bundle: nil)
let rootVC = storyboard.instantiateViewControllerWithIdentifier("HomeVC") as! UITabBarController
if PFUser.currentUser() != nil {
rootVC.selectedIndex = 2 // Index of the tab bar item you want to present, as shown in question it seems is item 2
self.window!.rootViewController = rootVC
}
回答by Sohil R. Memon
In order to push to the specific view controller, you must replace below line:
为了推送到特定的视图控制器,您必须替换以下行:
Yours:
你的:
rootVC?.navigationController?.pushViewController(settingsTableVC, animated: false)
Mine:
矿:
rootVC?.visibleViewController.navigationController?.pushViewController(settingsTableVC, animated: true)
And for presenting any view controller, you must use this:
为了呈现任何视图控制器,你必须使用这个:
rootVC?.visibleViewController.navigationController?.presentViewController(settingsTableVC, animated: false, completion: nil)