- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 SDL 应用程序中创建 NSTouchBar,并且需要将响应程序附加到 NSWindow 对象(这是 SDL 提供给 Cocoa 窗口系统的唯一访问权限)。
https://developer.apple.com/reference/appkit/nstouchbar
If you explicitly adopt the NSTouchBarProvider protocol in an object, you must also explicitly send the associated key-value observing notifications within NSTouchBar methods; this lets the system respond appropriately to changes in the bar.
这是什么意思以及我该如何做?我看到很多有关如何订阅通知的文档,但没有看到如何发送通知?
现在我有:
@interface MyTouchBarResponder : NSResponder <NSTouchBarDelegate>
- (id)init;
- (NSTouchBar *)makeTouchBar;
- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier;
@property(strong, readonly) NSTouchBar *touchBar;
@end
我用我在这里问的上一个问题的代码将它附加到窗口:How to create an NSTouchBar from an NSWindow object?
touchBarResponder.nextResponder = window.nextResponder;
window.nextResponder = touchBarResponder;
但我的回调从未被调用(我将 exit(0)
放入其中以使其非常明显)。当我将代码直接侵入 SDL 库时,一切都会按预期工作,但这不是一个可行的永久解决方案。
谢谢。
最佳答案
首先,您的自定义响应程序应符合 NSTouchBarProvider
(在上面,您声明了 touchBar
属性,但没有显式符合)
其次,您要确保您的自定义响应程序位于窗口的响应程序链中(无论是第一个响应程序还是链中的后一个响应程序)。使用上述代码调整响应者链后,您需要调用 -makeFirstResponder:
并传入窗口中的某个 View (如果您需要该 View 作为第一响应者)或使用自定义响应者对象。然后,您应该验证窗口的 firstResponder
是否是该对象。
有了这些,在窗口显示并成为关键之后,您应该至少调用一次 touchBar
。
要回答有关键值观察通知的问题,当您想要更改从 touchBar
返回的实际 NSTouchBar 对象时需要这样做。在一般情况下,这是不必要的,因为在静态触摸栏情况下是不必要的,即使在动态情况下,您也可以依靠在之前创建的触摸栏上设置 defaultItemIdentifiers
来实现将会更新。但是,如果您需要更改触摸栏对象,则需要确保在以下情况下为 touchBar
发送 -willChangeValueForKey:
和 -didChangeValueForKey:
:你改变了返回值。 This developer documentation on KVO更详细。
关于objective-c - 如何从 NSResponder 显式发送键值观察通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40701412/
我对 NSResponder 中的 -showContextHelp: 方法很好奇。 文档指出: Implemented by subclasses to invoke the help system
我有一个带有一些 NSTextField 的 Mac 应用程序(SDK 10.10): 由于我需要在文本字段获得和放弃焦点时收到通知,因此我对 NSTextField 进行了子类化: @interfa
我有一个带有 NSToolbar 和内容 View 的 NSWindow。我已将跟踪区域设置为 View 的整个框架。 我希望当光标离开内容 View 并进入工具栏和/或窗口标题时触发 mouseEx
我正在编写一个针对 OS X Lion 和 Snow Leopard 的应用程序。我有一个观点,我想响应滑动事件。我的理解是,如果在我的自定义 View 中实现了该方法,三指滑动将调用 -[NSRes
我正在尝试实现 smartMagnify:在 NSView 中,但它从未被调用。我有 rotate:、magnify: 等,它们都被成功调用 - 我需要做什么才能收到此消息?我搜索了非常稀疏的文档,找
我正在尝试在 SDL 应用程序中创建 NSTouchBar,并且需要将响应程序附加到 NSWindow 对象(这是 SDL 提供给 Cocoa 窗口系统的唯一访问权限)。 https://develo
有没有关于-[NSResponder presentError:]如何的文档方法有效吗?我正在尝试在我的库中创建 NSError 实例,其 userInfo 值向用户显示尽可能多的有用信息。我现在过得
我正在尝试在我的模型中实现撤消/重做。所以我让我的模型类成为 NSResponder 的子类,然后实现了以下内容: 注意:此代码是根据评论后的更多研究编辑的 func setAnnotations(_
NSResponder 定义了许多与键盘操作相关的操作,例如 moveUp: , moveUpAndModifySelection:和 moveToBeginningOfLine: .除了不是通过按修
我在绘图应用程序中有一个 View ,当选择其他工具时,它需要以其他方式响应。我认为替换该 View 的 NSResonder 会比设置 View 的每个响应者方法调用新的响应者方法要好得多。这可能吗
在我的 Cocoa 应用程序中,应用程序菜单上有一个菜单项“删除”,它会向第一响应者发送一条 delete: 消息。现在,我将键盘焦点放在 NSOutlineView 上,在本例中它是第一响应者。如果
要求 我想检测的不是单个字符而是一系列字符来触发操作,例如,当用户在 NSView 子类中键入 'cat' 时 - 但不是'c'。 除了维护像 NSMutableArray 这样的东西,并在我的 ke
我有这条线调用-makeFirstResponder: [self.window makeFirstResponder:libraryViewController.imageBrowser]; ima
在下面的代码中,我使用 NSWindow 和 NSOpenGLView 初始化一个 NSViewController [a NSResponder],呈现 View 并尝试将 NSViewContro
为什么调用 moveLeft() 和 moveRight() 方法,我已经关闭了窗口 Controller 的第一响应者能力?我没有在其他地方添加任何代码,所以我显然在某处遗漏了一些东西......
看来,当我从 NSViewController 主视图中删除 subview 时,响应者链会被修改,并且新 View 永远不会收到第一个响应者。 View Controller 位于弹出窗口内。顶部有
我会先说我对整个编程场景非常陌生。尽管如此,我在我的第一个 OS X 应用程序(一个简单的“Paint”风格应用程序)中取得了一些进展。然而,在阅读基于文档的编程指南后,我留下了一些问题。 我当前的设
我正在使用 -interpretKeyEvents: 实现自定义文本框,并试图找出 moveBackward: 与 moveLeft: 和 moveForward: 与 moveRight: 之间的区
我正在使用 NSView 的自定义子类并通过 keyDown/keyUp 方法接收键盘事件,一切正常,除了按下“Cmd ⌘”键时, keyDown 事件正常触发,但 keyUp 事件永远不会出现。 在
我正在实现一个 NSControlTextEditingDelegate 协议(protocol),但我不知道我应该匹配哪个类/协议(protocol),例如命令选择器。 #selector(Whic
我是一名优秀的程序员,十分优秀!