简体   繁体   中英

Create 'Next' button from textfield to textfield - Swift 2

I have 3 textfields. I'm trying to make it have a Next button to go to from 1st to 2nd, and 2nd to 3rd, and a Done button once finished with the 3rd.

This is my code:

import Foundation
import UIKit
import Darwin

class View3on3 : UIViewController, UITextFieldDelegate {

@IBOutlet weak var APTeams: UITextField!
@IBOutlet weak var APRounds: UITextField!
@IBOutlet weak var APBreakers: UITextField!

@IBOutlet weak var ToAPBreak: UIButton!

override func viewDidLoad()
{
    super.viewDidLoad()
    initializeTextFields()

    APTeams.delegate = self
    APRounds.delegate = self
    APBreakers.delegate = self

    ToAPBreak.backgroundColor = UIColor.clearColor()
    ToAPBreak.layer.cornerRadius = 11
    ToAPBreak.layer.borderWidth = 1
    ToAPBreak.layer.borderColor = self.view.tintColor.CGColor
}


func initializeTextFields()
{
    APTeams.keyboardType = UIKeyboardType.NumberPad
    APRounds.keyboardType = UIKeyboardType.NumberPad
    APBreakers.keyboardType = UIKeyboardType.NumberPad

}

func textFieldShouldReturn(textField: UITextField) -> Bool {
    if (textField == APTeams){
        APRounds.becomeFirstResponder()
    } else if (textField == APRounds){
        APBreakers.becomeFirstResponder()
    } else if (textField == APBreakers){
        APBreakers.resignFirstResponder()
    } else {
        //etc//
    }
    return true
}

This isn't working at all. What do I have to do?

For NumberPad type keyboard types iOS not able to display the return key.So you may try like this.

func initializeTextFields()
{
    APTeams.keyboardType = UIKeyboardType.NumbersAndPunctuation
    APRounds.keyboardType = UIKeyboardType.NumbersAndPunctuation
    APBreakers.keyboardType = UIKeyboardType.NumbersAndPunctuation

  APTeams.returnKeyType = UIReturnKeyType.Next
  APRounds.returnKeyType = UIReturnKeyType.Next
  APBreakers.returnKeyType = UIReturnKeyType.Done

}

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