[英]How to pass data to a ViewController from push notification through the storyboard?
This is a fragment of my iOS app: 这是我的iOS应用程序的一个片段:
where the Tab Bar Controller
is my root controller followed by a Navigation Controller
followed by a Table View Controller
followed by a final View Controller
. Tab Bar Controller
是我的根控制器,其后是Navigation Controller
后跟Table View Controller
后跟最终的View Controller
。 When a push notification arrives, I need to pass some data to this final view controller (in order to fill those blank UITextView
and a UITextField
). 当推送通知到达时,我需要将一些数据传递给最终的视图控制器(为了填充那些空白的
UITextView
和UITextField
)。 This is the code I have written up to now but I don't know how to go on: 这是我到目前为止编写的代码,但我不知道如何继续:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
//let tab :UITabBarController = self.window?.rootViewController as! UITabBarController
//tab.selectedViewController = tab.viewControllers![3]
print("NOTIFICA \n")
print(userInfo)
let tab :UITabBarController = self.window?.rootViewController as! UITabBarController
tab.selectedViewController = tab.viewControllers![2]
}
This code is supposed to let me access to the 3-rd view controller of the tab bar controller but I need to go beyond it to the final view controller and pass userInfo
to it. 这段代码可以让我访问标签栏控制器的第3个视图控制器,但我需要超越它到最终的视图控制器并将
userInfo
传递给它。 How shall I do? 我该怎么办? Thanks to all.
谢谢大家。
UPDATE UPDATE
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
//let tab :UITabBarController = self.window?.rootViewController as! UITabBarController
//tab.selectedViewController = tab.viewControllers![3]
print("NOTIFICA \n")
print(userInfo)
application.applicationIconBadgeNumber = 0
application.cancelAllLocalNotifications()
NSNotificationCenter.defaultCenter().postNotificationName("MyNotificationReceived", object: userInfo)
}
class DynamicEventsViewController:UIViewController {
@IBOutlet weak var dynamicEventTitle:UITextField!
@IBOutlet weak var dynamicEventDescription:UITextView!
var eventTitle:String!
var eventDescription:String!
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.titleView = UIImageView(image: UIImage(named: "icons/bar.png"))
self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(DynamicEventsViewController.notificatonArrived(_:)), name: "MyNotificationReceived", object: nil)
self.dynamicEventTitle.text = self.eventTitle.uppercaseString
self.dynamicEventDescription.text = self.eventDescription
}
func notificatonArrived(notification: NSNotification) {
let userInfo:[NSObject : AnyObject] = notification.userInfo!
self.eventTitle = userInfo["aps"]!["alert"] as! String
self.eventDescription = userInfo["aps"]!["description"] as! String
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
}
I think there are two questions to this post. 我认为这篇文章有两个问题。
For #1, you will have to first determine how the user would normally go to the final view controller. 对于#1,您必须首先确定用户通常如何进入最终视图控制器。 Based on the storyboard he would switch to the third tab, select an item in the table view which would then load the final view controller.
根据故事板,他将切换到第三个选项卡,在表视图中选择一个项目,然后加载最终的视图控制器。 So you will have to do this programatically by switching the Tab controller to the third view controller, then getting the navigation controller and then simulating the selection of an item in the table view which will then load the final view controller.
因此,您必须以编程方式执行此操作,方法是将Tab控制器切换到第三个视图控制器,然后获取导航控制器,然后模拟表视图中项目的选择,然后加载最终视图控制器。
For #2 notifications may not work properly because the final view controller may not be loaded by the time the notification is sent. 对于#2通知可能无法正常工作,因为在发送通知时可能无法加载最终视图控制器。 Its hard to sync this.
很难同步这个。 In this case you can save the push notification payload in user defaults using
NSStandardUserDefaults
. 在这种情况下,您可以使用
NSStandardUserDefaults
将推送通知有效负载保存在用户默认值中。 Then in the final view controller you can check if there is any prefs already stored as a part of this flow and if so load it and populate the text field and text view with the values and delete the prefs so that next time it is loaded the values are not populated. 然后在最终视图控制器中,您可以检查是否已存储任何prefs作为此流的一部分,如果是,则加载它并使用值填充文本字段和文本视图并删除prefs以便下次加载时值未填充。
This kind of decouples the push notification and saving of the data to be preloaded in the final view controller from the actual loading of this data in the final view controller. 这种类型将推送通知和保存要在最终视图控制器中预加载的数据与最终视图控制器中此数据的实际加载分离。
Another option is to just load the final view controller as a modal view controller and pass the data to that controller at the time of receiving the notification itself. 另一种选择是将最终视图控制器加载为模态视图控制器,并在接收通知本身时将数据传递给该控制器。 Of course this is applicable only if your final view controller's data model doesn't require it to always comes from the table view controller.
当然,仅当最终视图控制器的数据模型不要求它始终来自表视图控制器时,这才适用。
You can use NSNotificationClass
, you can read a tutorial here . 您可以使用
NSNotificationClass
,您可以在这里阅读教程 。 Register the notification in (say) FinalViewController
在(比方说)
FinalViewController
注册通知
class FinalViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(FinalViewController.notificatonArrived(_:)), name: "MyNotificationReceived", object: nil)
}
func notificatonArrived(notification: NSNotification) {
//do you updation of labels here
let userInfo = notification.userInfo
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
}
And when you receive the notification the do this 当您收到通知时,请执行此操作
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print("NOTIFICA \n")
print(userInfo)
NSNotificationCenter.defaultCenter().postNotificationName("MyNotificationReceived", object: userInfo)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.