ios Swift UITextFieldShouldReturn Return Key Tap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24150932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:47:44  来源:igfitidea点击:

Swift UITextFieldShouldReturn Return Key Tap

iosxcodeuitextfieldswift

提问by kmiklas

(iOS8, Xcode6, Swift) Using Swift, how do I capture a tap on the "Return" button?

(iOS8, Xcode6, Swift) 使用 Swift,我如何捕捉对“返回”按钮的点击?

The doc at the following link specifies using the textFieldShouldReturnmethod:

以下链接中的文档指定使用该textFieldShouldReturn方法:

// Swift
@optional func textFieldShouldReturn(_ textField: UITextField!) -> Bool

Where I'm hung up is in the "_ textField" part. I've created the text field using Storyboard. How do I capture notifications for this specific text field? Do I need to create a new class and set it as a delegate for this text field? Do I assign the text filed a name, and then somehow hook into it?

我挂断的地方是在“_ textField”部分。我已经使用 Storyboard 创建了文本字段。如何捕获此特定文本字段的通知?我是否需要创建一个新类并将其设置为该文本字段的委托?我是否为提交的文本指定一个名称,然后以某种方式挂入其中?

https://developer.apple.com/documentation/uikit/uitextfielddelegate/1619603-textfieldshouldreturn

https://developer.apple.com/documentation/uikit/uitextfielddelegate/1619603-textfieldshouldreturn

回答by jayesh kavathiya

class ViewController: UIViewController,UITextFieldDelegate //set delegate to class

@IBOutlet var txtValue: UITextField //create a textfile variable

override func viewDidLoad() {
   super.viewDidLoad() 
   txtValue.delegate = self //set delegate to textfile 
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {   //delegate method
   textField.resignFirstResponder()
   return true
}

回答by sijones

Implement this function

实现这个功能

func textFieldShouldReturn(_ textField: UITextField) -> Bool {   //delegate method
   textField.resignFirstResponder()
   return true
}

And for delegate you can set using the Utilities pane / Connections Inspector / delegateand then drag to ViewController (yellow button in the storyboard)

对于委托,您可以使用Utilities 窗格/Connections Inspector/delegate 进行设置,然后拖动到 ViewController(故事板中的黄色按钮)

Then you do not need to set the delegate programmatically for every text field

然后您不需要为每个文本字段以编程方式设置委托

回答by Ben Gottlieb

You need to set an object as the text field's delegate. Usually that would be the view controller that the text field exists in. You don't need to inherit from any other class, or, strictly speaking, implement a delegate (but you could implement UITextFieldDelegate to make things a little clearer.)

您需要将一个对象设置为文本字段的委托。通常那将是文本字段所在的视图控制器。您不需要从任何其他类继承,或者,严格来说,实现一个委托(但您可以实现 UITextFieldDelegate 以使事情更清楚一些。)

回答by iOS

In Swift 4.2 and Xcode 10.1

在 Swift 4.2 和 Xcode 10.1 中

//UITextField delegate method
func textFieldShouldReturn(_ textField: UITextField) -> Bool {

    if textField == TF1 {
        textField.resignFirstResponder()//
        TF2.becomeFirstResponder()//TF2 will respond immediately after TF1 resign.
    } else if textField == TF2  {
        textField.resignFirstResponder()
        TF3.becomeFirstResponder()//TF3 will respond first
    } else if textField == TF3 {
        textField.resignFirstResponder()
    }
    return true
}