“走”的行动ios键盘的按钮 [英] Action of the "Go" button of the ios keyboard

查看:72
本文介绍了“走”的行动ios键盘的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将动作绑定到iOS中键盘的 Go 按钮?

How do I bind an action to the Go button of the keyboard in iOS ?

推荐答案

Objective-C



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

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

    return YES;
}

不要忘记将您输入此代码的类作为文本分配字段的委托。

Don't forget to assign the class you put this code in as the text field's delegate.

self.someTextField.delegate = self;

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

Or if you'd prefer to use UIControlEvents, you can do the following

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

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

请参阅 @Wojtek Rutkowski的回答,了解如何在Interface Builder中执行此操作。

See @Wojtek Rutkowski's answer to see how to do this in Interface Builder.

UITextFieldDelegate

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

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键盘的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆