首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用App更新视图控制器变量

用App更新视图控制器变量
EN

Stack Overflow用户
提问于 2016-10-09 04:42:11
回答 1查看 1.5K关注 0票数 1

因此,我基本上是试图显示在应用程序中收到的推送通知。当收到通知时,我试图更新视图控制器的文本。但是,每次我试图更新视图控制器时,都会得到一个错误:fatal error: unexpectedly found nil while unwrapping an Optional value

以下是我的应用程序委托代码片段:

代码语言:javascript
复制
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)
}

以下是我的视图控制器:

代码语言:javascript
复制
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.
}
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-09 04:58:19

编辑的SWIFT2.2:

您正在application: didReceiveRemoteNotification方法中创建一个新的application: didReceiveRemoteNotification实例。因此,您正在更新与您想要更新的不同FirstViewController实例的属性。您正在更新的那个没有显示。错误的出现是因为updateText()正在被调用,但是故事板没有初始化,所以notificationLabel仍然是零。

若要执行您正在尝试的操作,请使用通知post将通知传递给视图控制器,并将FirstViewController添加为观察者:

在您的AppDelegate中,将主体更改为:

代码语言:javascript
复制
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中,添加一个观察者来侦听通知帖子:

代码语言:javascript
复制
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中,添加一个处理通知帖子的方法:

代码语言:javascript
复制
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()函数。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39940093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档