如何制作视图的子控件将重点关注NSViewController中的Tab和Shift + Tab

我必须升级一个现有的应用程序,并将其现有的UI拆分为单独的NIB。我计划开始为所有分割的UI创建单独的NIB和NSViewController。现在问题是我的NSViewController没有响应键盘TAB和SHFIT + TAB事件,我只是希望我的NSViewController在用户单击TAB或SHIFT + TAB时在我动态加载的NSViewController视图中设置焦点在适当的子控件上。 谢谢, 编辑: 以下是我的要求。 我有三个子视图,我需要动态加载并在我的MainWindow占位符NSBox中使用NSPopupButton进行切换。 为了检查我已经创建了新的cocoa应用程序并向Window添加了一个NSPopupButton和NSBox,并加入了NSPopupButton和NSBox的出口。 其次,我创建了三个新的NSViewController,其中三个不同的NIB包含单独的自定义视图,其中包含两个或三个NSTextField的子控件。
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
}
在主app代理函数中,我将所有三个NSViewController添加到一个数组中,然后使用replaceSubview替换视图以替换占位符NSBox中的视图。 我已经在所有三个NSViewController中添加了以下代码,但我仍然没有通过按TAB或SHIFT + Tab键来关注子控件。
- (void)loadView {
    [super loadView];

    // store the responder that’s right after the view in the responder chain
    NSResponder *nextResponder = [[self view] nextResponder];

    // set the view controller (self) as the next responder after the view
    [[self view] setNextResponder:self];

    // set the stored responder as the next responder after the view controller
    [self setNextResponder:nextResponder];
}
    
已邀请:
即使
NSViewController
继承自
NSResponder
,Cocoa也不会自动将
NSViewController
实例添加到响应者链中。你需要自己做。 一种可能的解决方案是将视图控制器插入到它正在控制的视图和该视图的下一个响应者之间的响应器链中。例如,在视图控制器实现中,
- (void)loadView {
    [super loadView];

    // store the responder that’s right after the view in the responder chain
    NSResponder *nextResponder = [[self view] nextResponder];

    // set the view controller (self) as the next responder after the view
    [[self view] setNextResponder:self];

    // set the stored responder as the next responder after the view controller
    [self setNextResponder:nextResponder];
}
    

要回复问题请先登录注册