简体   繁体   中英

Swift next button change textfield

I have an app built with Swift and iOS8. In my ViewController I had two textfields.

I use this code:

func textFieldShouldReturn(textField: UITextField) -> Bool {
    if textField == self.textfield_A {
        self.textfield_B.becomeFirstResponder()
    }
    if textField == self.textfield_B {
        self.textfield_B.resignFirstResponder()
    }
    return true
}

The effect:

I select textfield_A and Press the Next Button -> the cursor jump to textfield_B

On textfield_B I press the Done Button -> the keyboard will be hidden.

All works fine.

But now I have made a change and the code doesn't work like this anymore.

I changed the textfield_A to a textView .

Any idea how I have to modify my code?

You have to add an extension, this is the extension for swift 3.0

extension UITextField {
class func connectFields(fields:[UITextField]) -> Void {
    guard let last = fields.last else {
        return
    }
    for i in 0 ..< fields.count - 1 {
        fields[i].returnKeyType = .next
        fields[i].addTarget(fields[i+1], action: #selector(UIResponder.becomeFirstResponder), for: .editingDidEndOnExit)
    }
    last.returnKeyType = .done
    last.addTarget(last, action: #selector(UIResponder.resignFirstResponder), for: .editingDidEndOnExit)
    }
} 

and after you add this line of code:

UITextField.connectFields(fields: [field1, field2, field3])

textFieldShouldReturn won't be called anymore for textfield_A now because is is a textview now, not a textfield.

Try adding this function

func textView(textView: UITextView!, shouldChangeTextInRange: NSRange, replacementText: NSString!) -> Bool {
    if(replacementText == "\n") {
        textView.resignFirstResponder()
        return false
    }
    return true
}

If you are using both textfield then below code might be helpful to you my friend

Make sure your text fields have their delegate set and implement the textFieldShouldReturn method. This is the method that is called when the user taps the return key (no matter what it looks like).

The method might look something like this:

func textFieldShouldReturn(textField: UITextField) -> Bool {
    if textField == self.field1 {
        self.field2.becomeFirstResponder()
    }

    return true
}

And do not forget to give Delegate

TextField.delegate = self

Using textField you can detect return key press by using textFieldShouldReturn: method. textView is a multi-line input so return key just adds a new line, so the easiest way to catch return key press is to implement UITextViewDelegate method:

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    if text == "\n" {
        textView.resignFirstResponder()
        return false
    }
    return true
}

You can do it this way in your doneButton action:

@IBAction func donePressed(sender: AnyObject) {

    if textV.resignFirstResponder() {      //check if cursor is at textView.
        textfield_B.becomeFirstResponder() //move it to your next textField.
    } else  {         
        textfield_B.resignFirstResponder() //else hide your keyboard.
    }
}

And your result will be:

在此输入图像描述

Hope it helps.

Swift 4.2

This is a More Generic Solution you can use this code with any amount of TextFields. Just inherit UITextFieldDelegate and update the Textfield Tag according to the order

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    let txtTag:Int = textField.tag

    if let textFieldNxt = self.view.viewWithTag(txtTag+1) as? UITextField {
        textFieldNxt.becomeFirstResponder()
    }else{
        textField.resignFirstResponder()
    }

    return true
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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