Objective c 强制NSTextField发送TEXTDIDEDITING
我创建了一个子类NSTextField,这样当用户编辑完字段时,文本字段就会失去焦点。我还设置了它,因此每当用户单击主视图时,这将导致文本字段失去焦点。这一切都很好。现在我想给这个子类添加一些额外的功能 我希望每次用户单击框外的任何位置时,textfield都会发送TextDiDendEdit。这包括当用户单击另一个UI组件时。我现在看到的行为是,当用户单击另一个UI组件(比如组合框)时,不会触发该操作。有没有办法强迫你这么做?除了手动添加它作为其他组件操作的一部分之外 任何帮助都将不胜感激 这是我的textdidediting函数的代码Objective c 强制NSTextField发送TEXTDIDEDITING,objective-c,macos,cocoa,subclass,nstextfield,Objective C,Macos,Cocoa,Subclass,Nstextfield,我创建了一个子类NSTextField,这样当用户编辑完字段时,文本字段就会失去焦点。我还设置了它,因此每当用户单击主视图时,这将导致文本字段失去焦点。这一切都很好。现在我想给这个子类添加一些额外的功能 我希望每次用户单击框外的任何位置时,textfield都会发送TextDiDendEdit。这包括当用户单击另一个UI组件时。我现在看到的行为是,当用户单击另一个UI组件(比如组合框)时,不会触发该操作。有没有办法强迫你这么做?除了手动添加它作为其他组件操作的一部分之外 任何帮助都将不胜感激 这
- (void)textDidEndEditing:(NSNotification *)notification
{
NSString *file = nil;
char c = ' ';
int index = 0;
[super textDidEndEditing:notification];
if ([self isEditable])
{
// is there a valid string to display?
file = [self stringValue];
if ([file length] > 0)
{
c = [file characterAtIndex:([file length] - 1)];
if (c == '\n') // check for white space at the end
{
// whitespace at the end... remove
NSMutableString *newfile = [[NSMutableString alloc] init];
c = [file characterAtIndex:index++];
do
{
[newfile appendFormat:@"%c", c];
c = [file characterAtIndex:index++];
}
while ((c != '\n') && (index < [file length]));
[self setStringValue:newfile];
file = newfile;
}
[[NSNotificationCenter defaultCenter]
postNotificationName:@"inputFileEntered" object:self];
}
}
// since we're leaving this box, show no text in this box as selected.
// and deselect this box as the first responder
[self setSelectedText:0];
[[NSNotificationCenter defaultCenter]
postNotificationName:@"setResponderToNil" object:self];
}
“setResponderToNil”通知是我的NSView子类的一部分:
- (void)setSelectedText:(int) length
{
int start = 0;
NSText *editor = [self.window fieldEditor:YES forObject:self];
NSRange range = {start, length};
[editor setSelectedRange:range];
}
- (void)setResponderToNil
{
AppDelegate *delegate = (AppDelegate *)[NSApp delegate];
[delegate.window makeFirstResponder:nil];
}
我想我找到了一个方法。这可能不是最有说服力的,但它似乎与我想要的行为类型有关 我将鼠标事件侦听器添加到应用程序的主控制器:
event_monitor_mousedown_ = [NSEvent addLocalMonitorForEventsMatchingMask:NSRightMouseDown
handler:^NSEvent *(NSEvent * event)
{
NSResponder *resp = [[[NSApplication sharedApplication] keyWindow] firstResponder];
if ([resp isKindOfClass:[NSTextView class]])
{
// set UI in proper state - remove focus from text field
// even when touching a new window for the first time
[[NSNotificationCenter defaultCenter]
postNotificationName:@"setResponderToNil" object:self];
[self setStopState];
}
return event;
}];
此事件检查应用程序中的当前响应程序是否执行任何mouseDown操作。如果它是textView对象(这是编辑NSTextField时第一个响应程序的对象类型),它将发送将第一个响应程序设置为nil的通知。这将强制执行textdidediting通知。我想再玩一玩,看看我是否得到了正确的预期行为。我希望这能帮助别人 您希望仅当用户在文本字段外单击时,或者当用户在文本字段外与UI交互时,会发生任何其他事件。因此,如果用户单击按钮或与下拉框交互,我希望触发textDidEndEditing。我只是不想把它作为另一个UI组件操作的一部分。您可以为文本字段添加本地鼠标事件监视器进行检查,它将在每次单击时触发。您将把事件监视器放在哪里?如果您在文本字段中放置鼠标按下处理程序,它将仅在用户单击该字段时触发,而不是在用户单击外部时触发。还是我误解了活动的运作方式?