简体   繁体   English

如何通过故事板将推送通知中的数据传递给ViewController?

[英]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 ). 推送通知到达时,我需要将一些数据传递给最终的视图控制器(为了填充那些空白的UITextViewUITextField )。 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. 我认为这篇文章有两个问题。

  1. How to take the user to the final view controller upon receiving a push notification? 如何在收到推送通知后将用户带到最终视图控制器?
  2. How to pass the payload of the push notification to the final view controller? 如何将推送通知的有效负载传递给最终视图控制器?

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.

相关问题 如何从故事板中的appdelegate推送viewcontroller - How to push viewcontroller from appdelegate in storyboard ios将数据从TabbarController传递到Storyboard中的ViewController - ios Pass data from TabbarController to ViewController in Storyboard 使用Storyboard时不通过Storyboard推送ViewController - Push a ViewController not through Storyboard while working with Storyboard 如何在导航控制器内的storyboard中从appdelegate推送viewcontroller - How to push viewcontroller from appdelegate in storyboard inside navigation controller 将从推送通知接收的数据从AppDelegate传递到ViewController - Passing data received from a push notification from the AppDelegate to a ViewController iOS - 从代码和故事板推送viewController - iOS - Push viewController from code and storyboard 通过推式通知打开ViewController时如何管理视图层次结构 - how to manage view hierarchy when opening a viewcontroller from push notification 如何弹出viewController,然后通过委托推送viewController - How to pop a viewController and then Push a viewController through delegation 将信息传递到故事板上的ViewController - Pass info to viewcontroller on storyboard 如何从PushNotification传递notification.userinfo到Swift中的特定ViewController - How to pass notification.userinfo from PushNotification to a specific viewcontroller in Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM