【发布时间】:2016-12-29 16:04:02
【问题描述】:
我正在尝试添加支持,以通过插件将NSTouchBar 按钮暴露给我无法以其他方式修改的应用程序。该插件是一个共享库,在创建主窗口后在运行时加载。我创建了一个AppDelegate,如下所示:
@interface AppDelegate : NSResponder <NSTouchBarDelegate>
@end
使用实现makeTouchBar 和touchBar 函数的@implmentation:
- (NSTouchBar *) makeTouchBar
- (nullable NSTouchBarItem *) touchBar:(NSTouchBar *)touchBar
makeItemForIdentifier: (NSTouchBarItemIdentifier)identifier
我终于尝试将它注入到应用程序中,我在onload 函数中有以下内容,该函数在加载到应用程序时在动态库中调用。
NSWindow *w = [NSApp mainWindow];
AppDelegate *a = [[AppDelegate alloc] init];
a.nextResponder = w.nextResponder;
w.nextResponder = a;
// Re-setting FirstResponder will trigger AppDelegate::makeTouchBar
// that was attached above.
NSResponder *n = w.firstResponder;
[w makeFirstResponder: nil];
[w makeFirstResponder: n];
但是...AppDelegate::touchBar 永远不会被调用,因此我的测试按钮永远不会填充触摸栏。
如果我在 Xcode 中创建一个新项目,并使用完全相同的 AppDelegate 实现(复制粘贴相同的 @interface 和 @implementation),我会得到既显示又响应按下事件的功能按钮,所以似乎是注射部分坏了。 (我猜在 Xcode 中,一切都是通过 MainMenu.xib 连接起来的)
更新: 主要问题是应用程序的编译方式阻止了 TouchBar 工作。可能是基于旧版 Mac OS 构建的。
【问题讨论】:
标签: objective-c xcode cocoa macos-sierra nstouchbar