我对 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/