2009-06-10 39 views

回答

198
- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    [textField resignFirstResponder]; 
    return NO; 
} 

不要忘记设置在故事板的委托......

enter image description here

+2

不知怎的,这并不在所有的工作。回车键没有被捕获。 – Jonny 2010-12-16 02:07:20

+43

确保您的视图控制器设置为文本框的代表。 – zekel 2011-02-15 22:01:41

37

不需要代表团,这里是一个班轮:

- (void)viewDidLoad { 
    [textField addTarget:textField 
        action:@selector(resignFirstResponder) 
     forControlEvents:UIControlEventEditingDidEndOnExit]; 
} 

可悲的是,你不能直接在你的Storyboard中做到这一点(你不能将动作连接到发出它们的控件故事板),但你可以通过中介行动。

-1
- (BOOL)textFieldShouldReturn:(UITextField *)txtField 
{ 
[txtField resignFirstResponder]; 
return NO; 
} 

当输入按钮被点击时,这个委托方法被调用。你可以从这个委托方法捕获返回按钮。

0

SWIFT 3.0

override open func viewDidLoad() { 
    super.viewDidLoad()  
    textField.addTarget(self, action: #selector(enterPressed), for: .editingDidEndOnExit) 
} 

在enterPressed()函数把你所有的行为后

func enterPressed(){ 
    //do something with typed text if needed 
    textField.resignFirstResponder() 
} 
0

您现在可以这是故事板使用发送事件 '真的结束退出时' 做。

在您的视图控制器子类:

@IBAction func textFieldDidEndOnExit(textField: UITextField) { 
    textField.resignFirstResponder() 
} 

在你的故事板所希望的文本框:

enter image description here