gpt4 book ai didi

ios - ios键盘 "Go"按钮的 Action

转载 作者:IT王子 更新时间:2023-10-29 08:04:17 25 4
gpt4 key购买 nike

如何将操作绑定(bind)到 iOS 键盘的 Go 按钮?

最佳答案

objective-C

假设您使用的是 UITextField,您可以使用 <UITextFieldDelegate>方法 textFieldShouldReturn .

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder]; // Dismiss the keyboard.
// Execute any additional code

return YES;
}

不要忘记将您放入此代码的类指定为文本字段的委托(delegate)。

self.someTextField.delegate = self;

或者如果您更喜欢使用 UIControlEvents,您可以执行以下操作

[someTextField addTarget:self action:@selector(textFieldDidReturn:) forControlEvents:UIControlEventEditingDidEndOnExit];

- (void)textFieldDidReturn:(UITextField *)textField
{
// Execute additional code
}

参见 @Wojtek Rutkowski's answer了解如何在 Interface Builder 中执行此操作。

swift

UITextFieldDelegate

class SomeViewController: UIViewController, UITextFieldDelegate {
let someTextField = UITextField()

override func viewDidLoad() {
super.viewDidLoad()

someTextField.delegate = self
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder() // Dismiss the keyboard
// Execute additional code
return true
}
}

UIControlEvents

class SomeViewController: UIViewController {
let someTextField = UITextField()

override func viewDidLoad() {
super.viewDidLoad()

// Action argument can be Selector("textFieldDidReturn:") or "textFieldDidReturn:"
someTextField.addTarget(self, action: "textFieldDidReturn:", forControlEvents: .EditingDidEndOnExit)
}

func textFieldDidReturn(textField: UITextField!) {
textField.resignFirstResponder()
// Execute additional code
}
}

关于ios - ios键盘 "Go"按钮的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13065868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com