gpt4 book ai didi

ios - 设置 userActivity 时,我们是否必须覆盖 updateUserActivityState

转载 作者:行者123 更新时间:2023-11-28 07:24:50 27 4
gpt4 key购买 nike

关于 updateUserActivityState 的文档对我来说有点模糊。

https://developer.apple.com/documentation/uikit/uiresponder/1621095-updateuseractivitystate

Subclasses override this method to update the state of the given user activity. You should add state representing the user's activity into the NSUserActivity object using its addUserInfoEntries(from:) method.

When the state is dirty, you should set the needsSave property of the NSUserActivity to true, and this method will be called at an appropriate time.

所以这个方法在我们做的时候被调用:

open override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

self.userActivity?.needsSave = true
}

然后我们可以相应地更新事件:

override func updateUserActivityState(activity: NSUserActivity) {

activity.addUserInfoEntriesFromDictionary(someInfo)
super.updateUserActivityState(activity)
}

但是当我们只设置一次事件并且永远不会更新它时,我们是否也必须始终实现 updateUserActivityState

例如在 viewDidLoad 中设置一次事件时:

override open func viewDidLoad() {
super.viewDidLoad()

let activity = NSUserActivity(activityType: "com.myApp")
activity.title = "Viewing"
activity.userInfo = ["myApp.item.key": ["Some", "Things"]]
self.userActivity = activity
self.userActivity?.becomeCurrent()
}

最佳答案

But when we only set the activity once and never update it, should we also always have to implement updateUserActivityState?

如果 NSUserActivity 对象永远不会改变,并且在创建时已完全配置,则无需重写 UIResponder.updateUserActivityState 方法。

但是,为了保持一致性和面向 future (如果您确实使该用户事件对象可变),当响应者链收到更改通知时,应用可能仍希望使用 updateUserActivityState 来填充事件。

关于ios - 设置 userActivity 时,我们是否必须覆盖 updateUserActivityState,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56900461/

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