NSTextField resigning first responder

给你一囗甜甜゛ 提交于 2019-12-13 13:28:44

问题


I'm trying to have my text field resign first responder when the user is done editing it, like when they hit enter. The delegate method is getting called but what I have now doesn't work, what is the proper way to go about doing this?

- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor
{
    [fieldEditor resignFirstResponder];
    return YES;
}

回答1:


From the docs on the -resignFirstResponder method:

Use the NSWindow makeFirstResponder: method, not this method, to make an object the first responder. Never invoke this method directly.

This code should do it:

[myWindow makeFirstResponder:nil];



回答2:


- (void)controlTextDidEndEditing:(NSNotification *)obj {

   [self.window makeFirstResponder:view];
}

where "view" is the parent NSView of your NSTextView control.




回答3:


I believe you are looking for the NSControl delegate method:

- (void)controlTextDidEndEditing:(NSNotification *)notif

Implement this method in your NSTextField delegate (easily set in IB).

Then your request to change the responder, such as

[self.window selectNextKeyView:self]

should work.




回答4:


I am no expert in iPhone programming yet but the method you need to implement is "textFieldShouldReturn" implemented like this:

  - (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    [theTextField resignFirstResponder];
    return YES;
}


来源:https://stackoverflow.com/questions/4431068/nstextfield-resigning-first-responder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!