Ios 使用App Delegate更新视图控制器的变量

Ios 使用App Delegate更新视图控制器的变量,ios,swift,firebase,firebase-cloud-messaging,Ios,Swift,Firebase,Firebase Cloud Messaging,因此,我基本上是在尝试显示应用程序中接收到的推送通知。 我试图在收到通知时更新视图控制器的文本。但是,每次尝试更新视图控制器时,都会出现一个错误:致命错误:在展开可选值时意外发现nil 以下是我的appdelegate代码段: func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchComple

因此,我基本上是在尝试显示应用程序中接收到的推送通知。 我试图在收到通知时更新视图控制器的文本。但是,每次尝试更新视图控制器时,都会出现一个错误:
致命错误:在展开可选值时意外发现nil

以下是我的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"]!)")



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

我真的不知道为什么它会给我这个错误。也许有人能解释一下,帮我解决这个问题。感谢您的帮助

为Swift 2.2编辑:

您正在
应用程序:didReceiveMemotentification
方法中创建
FirstViewController
的新实例。因此,您正在更新与您想要更新的不同的
FirstViewController
实例的属性。不显示您正在更新的文件。出现错误是因为正在调用
updateText()
,但情节提要尚未初始化,因此notificationLabel仍然为零

要执行您试图执行的操作,请使用通知张贴将通知传递给视图控制器,并将
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()函数。

是否使用界面生成器?@Kex情节提要?Yes@NathanModern这是因为您没有从故事板为viewcontroller创建对象。您希望在推送到达时打开此视图控制器,还是在推送到达时您在该视图控制器上?您正在app main中创建视图控制器,但实际上没有引用在情节提要中创建的视图控制器。我希望在收到推送通知时更新视图控制器,以便在应用启动时更新在应用程序关闭时处于活动状态。我希望它显示通知文本。我将情节提要连接到ViewController@Kex@Ian摩西这将创建一个新的控制器,每次即使应用程序是active@NathanModern这可能是让视图控制器处理从appDelegate接收到的通知的正确方法。@我应该在哪里添加viewController观察者?我通常在视图控制器的ViewWillAspect方法中添加观察者。您能将其转换为吗swift 2.2?