xcode 如何使用键盘“Go”在 Swift 2 中像 UIButton 一样提交 UITextFields

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31157847/
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-09-15 07:24:22  来源:igfitidea点击:

How to use the keyboard "Go" to submit UITextFields like a UIButton in Swift 2

iosxcodeswiftios8swift2

提问by Sin

Ok so I have a function that allows my user to use the keyboard to go to the next field (Which I got the code from SO) It works perfect. My problem is, once my user gets to the final text field, in which, I've selected "GO" as the return button, and I want to use the go as the ideal submit button. As the flow through the form goes, its presentably right, but the functionality at the end isn't there. I found an answer here, but it looks like its calling the same functions as the "next field" code. So they don't work well together. So here's what the Next Field code looks like:

好的,所以我有一个功能,允许我的用户使用键盘转到下一个字段(我从 SO 那里获得了代码)它工作得很好。我的问题是,一旦我的用户进入最后一个文本字段,我选择了“GO”作为返回按钮,并且我想使用 go 作为理想的提交按钮。随着表单的流逝,它看起来是正确的,但最后的功能不存在。我在这里找到了答案,但看起来它调用的函数与“下一个字段”代码相同。所以他们不能很好地协同工作。下面是 Next Field 代码的样子:

override func viewDidLoad() {
    super.viewDidLoad()
    // Keyboard Next Field & Delegate
    enterEmailTextField.delegate = self
    enterPasswordTextField.delegate = self
    self.enterEmailTextField.nextField = self.enterPasswordTextField
    self.enterPasswordTextField.nextField = self.enterNameTextField
    // ...
}

And the next block is displayed below override (which i'm sure you know) func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }

下一个块显示在覆盖下面(我相信你知道) func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }

// Next Field
func textFieldShouldReturn(textField: UITextField) -> Bool {
    if let nextField = textField.nextField {
        nextField.becomeFirstResponder()
    }

    return true
}

Ok that works just fine down to the last text field, But since the last field is "Go" I'm trying to mimic what I would do with say an @IBAction for a button, but instead the go button. Here's where I got the idea/code for the Go to work:

好的,直到最后一个文本字段都可以正常工作,但是由于最后一个字段是“Go”,我试图模仿我对按钮的@IBAction 所做的事情,而不是 go 按钮。这是我获得 Go to work 的想法/代码的地方:

Action of the "Go" button of the ios keyboard

ios键盘“Go”按钮的动作

Any Ideas on how to implement these? Or maybe just to work with the next function, and implement a "keyboard go" action similar to an @IBaction? Maybe just an all around better way to implement both of these so they coincide with each other? Any help is much appreciated!

关于如何实现这些的任何想法?或者只是为了使用下一个函数,并实现类似于@IBaction 的“键盘运行”操作?也许只是实现这两者的全面更好的方法,使它们彼此一致?任何帮助深表感谢!

EDIT!

编辑!

I forgot the actual NextField.swift File which I'm sure is important (sorry)

我忘记了实际的 NextField.swift 文件,我确信它很重要(抱歉)

import Foundation
import UIKit

private var kAssociationKeyNextField: UInt8 = 0


extension UITextField {
    @IBOutlet var nextField: UITextField? {
        get {
            return objc_getAssociatedObject(self,     &kAssociationKeyNextField) as? UITextField
        }
         set(newField) {
            objc_setAssociatedObject(self, &kAssociationKeyNextField,     newField, UInt(OBJC_ASSOCIATION_RETAIN))
        }
    }
}

this would help a lot I'm assuming

我假设这会很有帮助

回答by Christian Schnorr

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.returnKeyType == UIReturnKeyNext) {
        // tab forward logic here
        return YES;
    }
    else if (textField.returnKeyType == UIReturnKeyGo) {
        // submit action here
        return YES;
    }

    return NO;
}

On a cleaner way to handle tab order and form submitting, read my answer here.

以更简洁的方式处理 Tab 键顺序和表单提交,请在此处阅读我的回答。

回答by kirti Chavda

First set return Key Go of your textfield then implement following Method

首先设置您的文本字段的 return Key Go 然后实现以下方法

func textFieldShouldReturn(textField: UITextField) -> Bool {
      if (textField.returnKeyType==UIReturnKeyType.Go)
         {
            //implemnt yor Ibaction method here
        }
      return true
 }

then see in below imgae: enter image description here

然后在下面的imgae中看到: 在此处输入图片说明