当输入键被按下时,在objective-c中执行代码?
问题描述:
我正在使用Xcode 6中的Objective-C Xcode项目。我有一个文本框和一个按钮,用于在按下时检查文本框中的内容。当输入键被按下时,在objective-c中执行代码?
但是,我希望代码在按下输入键而不是提交按钮时运行。
我已经看到了你可以设置在Xcode按钮等效键但使用此选项不会出现在Xcode 6
答
实现textField委托方法。
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self callYourCodeWantToExecute];
//return YES; // want to hide keyboard
//return NO; // want keyboard
}
答
到功能您需要:
- 设置委托文本框
- 在您的委托对象句柄
-(BOOL)textFieldShouldReturn:(UITextField *)textField
即做什么,你需要做什么时,返回按下并返回YES
如果你想要文本字段得到返回NO
如果你不想处理文本字段的返回。
答
首先做出的UITextField文件的委托,然后此方法添加到您的代码..
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
答
按照下面的步骤。
- 的UITextField
- 添加标签值的设置代表如果有多个文本框
- 现在叫的UITextField的委托方法下同。
斯威夫特
optional func textFieldShouldReturn(_ textField: UITextField) -> Bool
希望这有助于!
成为UITextField的代表,然后你会得到 - [textFieldShouldReturn:] –