【问题标题】:How to control TAB key press event on NSPopUpButton programmatically如何以编程方式控制 NSPopUpButton 上的 TAB 按键事件
【发布时间】:2014-08-22 22:50:41
【问题描述】:

我正在使用 Xcode5 为 MacOSX 开发应用程序

我想控制当我在选定的 NSPopUpButton 上按 Tab 时,我创建了自己的 NSPopUpButton 子类,命名为 MyNSPopUpButton 并输入:

-(BOOL)canBecomeKeyView{
    return YES;
}

现在我无法通过按 Tab 键选择我的 NSPopupButtons,但我还想控制从那里去哪里,在 NSTextFields 上我使用这种方法:

-(BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector{

    NSTextField     *textField  = (NSTextField *) control;

    if (commandSelector == @selector(insertTab:) ||
        commandSelector == @selector(insertNewline:)) {


        if (textField == _diaTextField) {
            [self.window makeFirstResponder:_cantidadDebeTextField];
        }

        if (textField == _cantidadDebeTextField) {
            [self.window makeFirstResponder:_cuentaDebePopupButton];
        }

        if (textField == _cantidadHaberTextField) {
            [self.window makeFirstResponder:_descripcionAsientoTextField];
        }

        if (textField == _descripcionAsientoTextField) {
            [self.window makeFirstResponder:_descripcionBitacoraTextField];
        }

        if (textField == _descripcionBitacoraTextField) {
            [self.window makeFirstResponder:nil];
        }

        return YES;
    }
    return NO;
}

如您所见,我正在使用 TAB 键控制导航,我想对 NSPopUpButtons 做同样的事情,但我什至不能像使用 NSTextFields 那样设置它们的任何代表,

我知道我可以通过按正确顺序设置标签编号来做到这一点,但我会以编程方式在运行时添加更多控件

有没有办法通过代码来控制这个???

感谢支持

【问题讨论】:

    标签: macos xcode5.1 nspopupbutton


    【解决方案1】:

    您关闭窗口上的autorecalculatesKeyViewLoop 并自己设置键视图循环。您可以通过设置所有视图的 nextKeyView 属性来做到这一点。它们应该形成一个循环。

    您可能也想设置窗口的initialFirstResponder。

    添加视图时,您需要调整关键视图循环以将其包含在所需位置。

    【讨论】:

    • autorecalculatesKeyViewLoop 我默认关闭它,我也有我的第一个视图 [_diaTextField becomeFirstResponder];当我加载视图时,关键视图循环何时是静态的,它具有正确的顺序,但是当我添加更多控件时它会丢失顺序,这就是为什么我需要以编程方式设置顺序,使用 NSTextFields 一切正常,但是我无法以编程方式使用 NSPopupButtons 设置顺序 :(
    • 首先,您永远不应该调用-becomeFirstResponder(除非在覆盖范围内调用super)。在NSResponder 上查看该方法的文档。设置窗口的initialFirstResponder 或调用-[NSWindow makeFirstResponder:]。接下来,如果您在窗口中添加或删除视图,并且您正在手动管理关键视图循环,则需要酌情调用-setNextKeyView: 以合并新视图或关闭删除视图留下的任何间隙。您为文本字段展示的技术是一种非常迂回的方法,在一般情况下不起作用。设置nextKeyView 将起作用。
    猜你喜欢
    • 2013-08-12
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    相关资源
    最近更新 更多