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

标签 ios swift cocoa-touch uiresponder nsuseractivity

关于 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/

相关文章:

iphone - Root.plist 文件不可编辑

ios - 如何通过按下按钮从数组中随机快速打印一个简单的引号?

iphone - 如何在 UIDatePicker 中禁用 AM/PM

ios - 使用 XCTest 点击 UI 元素时如何避免 NSInternalInconsistencyException?

iphone - 添加 'crash screen'吗?

iPhone 应用程序作为网络服务器,有什么建议吗?

ios - 接受 hh :mm format 中文本字段的时间输入的正则表达式应该是什么

ios - Firebase .childAdded 和事件指示器

ios - 嵌入式 UINavigationController 的后退按钮不起作用

ios - swift 与Firebase聊天