gpt4 book ai didi

ios - 为什么数据没有被保存?

转载 作者:行者123 更新时间:2023-11-29 01:22:13 26 4
gpt4 key购买 nike

当我尝试在某个 View Controller 上打开通知时,出现 fatal error :在展开可选值时意外发现 nil。我想我知道为什么,有些变量为 nil,导致应用程序崩溃,但我试图为这些变量提供数据,但它们没有保存。

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
PFPush.handlePush(userInfo)
let text = userInfo["aps"]!["alert"]
let title = userInfo["aps"]!["alert"]!!["title"]
let artist = userInfo["aps"]!["alert"]!!["artist"]
print(text)
print(title)
print(artist)

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("PlayerController") as! PlayerViewController
vc.dataModel.artistplay = artist as? String
vc.dataModel.titleplay = title as? String
window?.rootViewController = vc

}

这是 PlayerViewController 的代码(我在打开推送通知时尝试打开的 View Controller )

 @IBOutlet weak var playertitle: UILabel!
@IBOutlet weak var playerartist: UILabel!
var dicData : NSDictionary?
var dataModel : DataModelTwo?
var data: NSDictionary?
var shareDataModel: Share?
var buttonState: Int = 0;

override func viewWillAppear(animated: Bool) {

playertitle.text = dataModel!.titleplay
playerartist.text = dataModel!.artistplay

}

最佳答案

不要从 AppDelegate 设置 UILable 文本。在您的 ViewController 中设置一个变量,然后在 viewDidLoad 中使用它。

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
PFPush.handlePush(userInfo)
let text = userInfo["aps"]!["alert"]
print(text)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("PlayerController") as! PlayerViewController
vc.foo = "bar"
window?.rootViewController = vc
}

使用变量和更新 UI :

override func viewDidLoad() {
print(self.foo) // output is "bar"
}

关于ios - 为什么数据没有被保存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34466923/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com