因此,我基本上是试图显示在应用程序中收到的推送通知。当收到通知时,我试图更新视图控制器的文本。但是,每次我试图更新视图控制器时,都会得到一个错误:fatal error: unexpectedly found nil while unwrapping an Optional value
以下是我的应用程序委托代码片段:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// Print message ID.
print("Message ID: \(userInfo["gcm.message_id"]!)")
let vc = FirstViewController()
vc.notificationText = "Test"
print (vc.notificationText)
vc.updateText()
// Print full message.
print("%@", userInfo)
}以下是我的视图控制器:
class FirstViewController: UIViewController {
@IBOutlet weak var notificationLabel: UILabel!
var notificationText = "No notifications"
@IBAction func logOut(sender: AnyObject) {
try! FIRAuth.auth()!.signOut()
LogOutComplete()
}
func LogOutComplete() {
let mainStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let logIn : UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier("LogIn") as UIViewController
self.presentViewController(logIn, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
notificationLabel.text = notificationText
//notificationLabel.text = notificationText
// Do any additional setup after loading the view.
}
/*override func viewWillAppear(animated: Bool) {
notificationLabel.text = notificationText
}*/
func updateText () {
notificationLabel.text = notificationText
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}我真的不知道它为什么会给我这个错误。也许有人能解释并帮助我解决这个问题。感谢所有的帮助!
发布于 2016-10-09 04:58:19
编辑的SWIFT2.2:
您正在application: didReceiveRemoteNotification方法中创建一个新的application: didReceiveRemoteNotification实例。因此,您正在更新与您想要更新的不同FirstViewController实例的属性。您正在更新的那个没有显示。错误的出现是因为updateText()正在被调用,但是故事板没有初始化,所以notificationLabel仍然是零。
若要执行您正在尝试的操作,请使用通知post将通知传递给视图控制器,并将FirstViewController添加为观察者:
在您的AppDelegate中,将主体更改为:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// Print message ID.
print("Message ID: \(userInfo["gcm.message_id"]!)")
NotificationCenter.default.post(name: "Add_Notification_Post_Name_Here", object: nil,
userInfo: userInfo)}
在FirstViewController中,添加一个观察者来侦听通知帖子:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: Some_Method_Name:, name: "Add_Notification_Post_Name_Here", object: nil)
}在FirstViewController中,添加一个处理通知帖子的方法:
func Some_Method_Name(_ notification: NSNotification) {
guard let userInfo = notification.userInfo else {
return
}
guard let payload = userInfo["Some_Message_Key_Here"] as? NSString else {
return
}
notificationText = String(payload)
updateText()
}此方法将包转换为要分配给通知文本的字符串类型的有效负载。然后调用updateText()函数。
https://stackoverflow.com/questions/39940093
复制相似问题