加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

macos – 在Swift 3中使用NSUndoManager和.prepare(withInvocati

发布时间:2020-12-14 05:22:33 所属栏目:百科 来源:网络整理
导读:我正在将 Xcode 7 / Swift 2.2 mac OS X项目迁移到Xcode 8 / Swift 3,我在我的视图控制器类MyViewController中使用undoManager遇到了一个问题,它有一个函数undo. 在Xcode 7 / Swift 2.2中,这很好用: undoManager?.prepareWithInvocationTarget(self).undo(d
我正在将 Xcode 7 / Swift 2.2 mac OS X项目迁移到Xcode 8 / Swift 3,我在我的视图控制器类MyViewController中使用undoManager遇到了一个问题,它有一个函数undo.

在Xcode 7 / Swift 2.2中,这很好用:

undoManager?.prepareWithInvocationTarget(self).undo(data,moreData: moreData)
undoManager?.setActionName("Change Data)

在Xcode 8 / Swift 3中,使用https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html推荐的模式

这应该改为:

if let target = undoManager?.prepare(withInvocationTarget: self) as? MyViewController {
    target.undo(data,moreData: moreData)
    undoManager?. setActionName("Change Data")
}

但是,向MycastController的向下转换始终失败,并且未注册撤消操作.

我错过了一些明显的东西,或者这是一个错误?

prepareWithInvocationTarget(_:)(或在Swift 3中准备(withInvocationTarget :))创建一个隐藏的代理对象,Swift 3运行时无法正常工作.

(您可以将其称为错误,并发送错误报告.)

为了达到你的目的,你不能使用registerUndo(withTarget:handler :)吗?

undoManager?.registerUndo(withTarget: self) {targetSelf in
    targetSelf.undo(data,moreData: moreData)
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读