简体   繁体   English

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

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

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.好的,所以我有一个功能,允许我的用户使用键盘转到下一个字段(我从 SO 那里得到了代码)它工作得很好。 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.我的问题是,一旦我的用户进入最后一个文本字段,我选择了“GO”作为返回按钮,并且我想使用 go 作为理想的提交按钮。 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:下面是 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.好的,直到最后一个文本字段都可以正常工作,但是由于最后一个字段是“Go”,因此我试图模仿我对按钮说 @IBAction 的做法,而不是 go 按钮。 Here's where I got the idea/code for the Go to work:这是我获得 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?或者也许只是为了使用下一个函数,并实现类似于@IBaction 的“keyboard go”动作? Maybe just an all around better way to implement both of these so they coincide with each other?也许只是一个更好的方法来实现这两者,使它们彼此一致? Any help is much appreciated!任何帮助深表感谢!

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我假设这会很有帮助

- (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 键顺序和表单提交,请在此处阅读我的回答。

First set return key to Go of your TextField, then implement the following method:首先将返回键设置为 TextField 的 Go,然后实现以下方法:

func textFieldShouldReturn(textField: UITextField) -> Bool {
      if (textField.returnKeyType == UIReturnKeyType.Go)
         {
            // Implement your IBAction method here
        }
      return true
 }

then see in below image:然后见下图:

在此处输入图片说明

Available for iOS 15 in SwiftUI .适用SwiftUI 中的iOS 15 You can use .submitLabel(.go) to do it.您可以使用.submitLabel(.go)来做到这一点。

@available(iOS 15.0, *)
struct TextFieldSubmitView: View {
    @Binding var name: String

    var body: some View {
        VStack(alignment: .leading) {
            TextField("Type text", text: $name)
                .textFieldStyle(.roundedBorder)
                .padding()
            #if os(iOS)
                .submitLabel(.go)
            #endif
        }
    }
}

@available(iOS 15.0, *)
struct TextFieldSubmitView_Previews: PreviewProvider {
    static var previews: some View {
        TextFieldSubmitView(name: .constant("Name"))
            .previewLayout(.sizeThatFits)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM