ios - 如何通过 Storyboard 将数据从推送通知传递到 ViewController?

标签 ios swift push-notification apple-push-notifications

这是我的 iOS 应用程序的片段:

enter image description here

Tab Bar Controller 是我的根 Controller ,然后是 Navigation Controller,然后是 Table View Controller,最后是 >查看 Controller 。当 推送通知 到达时,我需要将一些数据传递给这个最终的 View Controller (以填充那些空白的 UITextViewUITextField) . 这是我到目前为止编写的代码,但我不知道如何继续:

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]
}

这段代码应该让我访问标签栏 Controller 的第 3 个 View Controller ,但我需要超越它到达最终的 View Controller 并将 userInfo 传递给它。我该怎么办?感谢大家。

更新

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. 如何在收到推送通知后将用户带到最终的 View Controller ?
  2. 如何将推送通知的负载传递给最终的 View Controller ?

对于#1,您必须首先确定用户通常如何进入最终 View Controller 。根据 Storyboard,他会切换到第三个选项卡,在 TableView 中选择一个项目,然后加载最终的 View Controller 。因此,您必须以编程方式执行此操作,方法是将选项卡 Controller 切换到第三个 View Controller ,然后获取导航 Controller ,然后模拟在 TableView 中选择一个项目,然后加载最终的 View Controller 。

对于 #2 通知可能无法正常工作,因为在发送通知时可能未加载最终 View Controller 。很难同步这个。在这种情况下,您可以使用 NSStandardUserDefaults 将推送通知负载保存在用户默认值中。然后在最终的 View Controller 中,您可以检查是否有任何首选项已作为此流程的一部分存储,如果是,则加载它并使用值填充文本字段和 TextView 并删除首选项,以便下次加载时值未填充。

这种方式将推送通知和保存要预加载到最终 View Controller 中的数据与最终 View Controller 中该数据的实际加载分离开来。

另一种选择是将最终 View Controller 加载为模态视图 Controller ,并在收到通知时将数据传递给该 Controller 。当然,这仅适用于最终 View Controller 的数据模型不要求它始终来自 TableView Controller 的情况。

关于ios - 如何通过 Storyboard 将数据从推送通知传递到 ViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36284459/

相关文章:

python - Gmail 推送通知 Python API

android - 当我收到通知时,我的应用程序在后台打开时只显示通知而不打开应用程序?

ios - 支付问题的 Siri 集成

iphone - 如何获取关系键

ios - XCTest 用于测试模型对象

swift - 如何在 SwiftUI 中从图库中选择图像

java - 无法理解消息 GCM

ios - 如何设置可访问性容器以使手势正常工作?

ios - 视网膜图像问题

swift - 将 nil 作为 UnsafePointer<UInt8> 传递