objective-c - 如何在我从该控件之一的子类中放置控件的 View 中使用方法

标签 objective-c cocoa nstextfield nsevent

我对 NSTextField 进行子类化,并在子类中使用事件方法(keyUp),当用户按 Enter 键时,我想在主视图(我将 NSTextFiled 放在其上的 View )中调用一个方法。我使用下面的代码,但不这样做如何在主视图中调用特定方法

- (void)keyUp:(NSEvent *)theEvent
{
    NSString *theArrow = [theEvent charactersIgnoringModifiers];
    unichar keyChar = 0;
    keyChar = [theArrow characterAtIndex:0];
    if ( keyChar == NSEnterCharacter )
    {
        //call method that exist in main view   
    }   
}

最佳答案

这应该可以...

SEL sel = @selector(textFieldDidPressEnter:);

if (self.delegate && [self.delegate respondsToSelector:sel]) {
    [self.delegate performSelector:sel withObject:self];
}

然后确保您想要接收此消息的类被设置为委托(delegate)。更完整的答案是将 textFieldDidPressEnter: 声明为委托(delegate)协议(protocol)的一部分。 Here's a good resource关于如何做到这一点。

关于objective-c - 如何在我从该控件之一的子类中放置控件的 View 中使用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10366801/

相关文章:

ios - AFNetworking 2.0 HTTP POST 进展

objective-c - 还有其他方法可以在iOS游戏开发中播放背景音乐吗?

ios - 如何检查NSString是否具有带有特定地址的URL

swift - 如何在 macOs 中禁用键盘输入?

xcode - NSTextField 在文本更改时调用 IBAction

objective-c - 如何在 Objective-C 的 NSTextField 中组合 char 和 int?

objective-c - 在 Objective-C 中更改预处理器值

objective-c - 是否可以在 Windows 和 Linux 中使用 Cocoa 进行开发?

objective-c - Objective-C "private"方法的命名约定是什么?

objective-c - 在菜单 View 中以编程方式选择文本