[英]How to pass data to a ViewController from push notification through the storyboard?
这是我的iOS应用程序的一个片段:
Tab Bar Controller
是我的根控制器,其后是Navigation Controller
后跟Table View Controller
后跟最终的View Controller
。 当推送通知到达时,我需要将一些数据传递给最终的视图控制器(为了填充那些空白的UITextView
和UITextField
)。 这是我到目前为止编写的代码,但我不知道如何继续:
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]
}
这段代码可以让我访问标签栏控制器的第3个视图控制器,但我需要超越它到最终的视图控制器并将userInfo
传递给它。 我该怎么办? 谢谢大家。
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)
}
}
我认为这篇文章有两个问题。
对于#1,您必须首先确定用户通常如何进入最终视图控制器。 根据故事板,他将切换到第三个选项卡,在表视图中选择一个项目,然后加载最终的视图控制器。 因此,您必须以编程方式执行此操作,方法是将Tab控制器切换到第三个视图控制器,然后获取导航控制器,然后模拟表视图中项目的选择,然后加载最终视图控制器。
对于#2通知可能无法正常工作,因为在发送通知时可能无法加载最终视图控制器。 很难同步这个。 在这种情况下,您可以使用NSStandardUserDefaults
将推送通知有效负载保存在用户默认值中。 然后在最终视图控制器中,您可以检查是否已存储任何prefs作为此流的一部分,如果是,则加载它并使用值填充文本字段和文本视图并删除prefs以便下次加载时值未填充。
这种类型将推送通知和保存要在最终视图控制器中预加载的数据与最终视图控制器中此数据的实际加载分离。
另一种选择是将最终视图控制器加载为模态视图控制器,并在接收通知本身时将数据传递给该控制器。 当然,仅当最终视图控制器的数据模型不要求它始终来自表视图控制器时,这才适用。
您可以使用NSNotificationClass
,您可以在这里阅读教程 。 在(比方说) 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)
}
}
当您收到通知时,请执行此操作
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.