[英]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.