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

时间:2011-02-16 06:53:57

标签: cocoa nsviewcontroller

我必须升级一个现有的应用程序,并且需要将其现有的UI拆分为单独的NIB。我计划开始为所有分割的UI创建单独的NIB和NSViewController。现在的问题是我的NSViewController没有响应键盘TAB和SHFIT + TAB事件,我只是希望我的NSViewController在用户单击TAB或SHIFT + TAB时在我动态加载的NSViewController视图中设置焦点适当的子控件。

谢谢,

已编辑: 以下是我的要求。

我有三个子视图需要动态加载,并在我的MainWindow占位符NSBox中使用NSPopupButton进行切换。

为了检查我已创建新的cocoa应用程序并将一个NSPopupButton和NSBox添加到Window并加入NSPopupButton和NSBox的出口。

其次,我创建了三个新的NSViewController,其中三个不同的NIB包含单独的自定义视图,其中包含两个或三个NSTextField的子控件。

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
}

在主app appate函数中,我将所有三个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];
}

1 个答案:

答案 0 :(得分:2)

即使NSViewController继承自NSResponder,Cocoa也不会自动将NSViewController个实例添加到responder chain。你需要自己做。

一种可能的解决方案是将视图控制器插入到它正在控制的视图和该视图的下一个响应者之间的响应器链中。例如,在视图控制器实现中,

- (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];
}