如何使视图的子控件将获得焦点在NSViewController中的Tab和Shift + Tab [英] How to make child controls of view will get focus on Tab and Shift+Tab in NSViewController

查看:531
本文介绍了如何使视图的子控件将获得焦点在NSViewController中的Tab和Shift + Tab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have to upgrade one existing application and neeed to split its existing UI into separate NIBs. I have planning to start with creating separate NIBs and NSViewController for all my splitted UIs. Now the problem is my NSViewController didn't respond on keyboard TAB and SHFIT+TAB events, I simply wants my NSViewController to set focus on appropriate child control in my dynamically loaded NSViewController view when user click TAB or SHIFT+TAB.

感谢,

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

EDITED : Following is my requirement.

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

I have three sub views which i need to load dynamically and switch using NSPopupButton in my MainWindow placeholder NSBox.

为了检查我已经创建了新的可可应用程序,并添加一个NSPopupButton和NSBox到窗口,并加入NSPopupButton和NSBox的插座。

For checking i have created new cocoa app and added one NSPopupButton and NSBox to Window and join the outlets for NSPopupButton and NSBox.

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

Second, I have created three new NSViewController's with three different NIB's containing separate custom view containing two or three NSTextField's child controls.

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

在主应用程序委托函数中,我将所有三个NSViewController添加到数组中,然后使用replaceSubview交换视图

In the main app delegate function i am adding all the three NSViewController's to an array and later swapping of views using replaceSubview to replace views in placeholder NSBox.

我在所有三个NSViewController中添加了以下代码,但是我仍然没有通过按TAB或SHIFT + tab键来关注子控件。 / p>

I have added following code in all the three NSViewController's, but i am still not getting focus on child controls by pressing TAB or SHIFT+tab keys.

- (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 添加到响应者链

Even though NSViewController inherits from NSResponder, Cocoa doesn’t automatically add NSViewController instances to the responder chain. You need to do it yourself.

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

One possible solution is to insert your view controller into the responder chain between the view it is controlling and the next responder of that view. For instance, in your view controller implementation,

- (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中的Tab和Shift + Tab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆