0

I searched everywhere and can't seem to find anything on this. For example I want to have more then one UITextField on the same screen. If I tap on one and type in information and click return it does an action. If you tap in the second one and return the keyboard it does a different action.

Any suggestions or help would be awesome thanks!

Jaydeep Vora
  • 6,085
  • 1
  • 22
  • 40
Sam
  • 3
  • 5
  • TextField Delegates is Answer **textFieldShouldReturn** or make use of **Notification Observers** to manage the state of Keyboard and perform Specific Action – iOS Geek Feb 27 '18 at 04:17

3 Answers3

0

One option is to use tags. In the example below you can select the next textfield, and only on the last one do we login. You could use cases to do whatever you'd like

//automatically move to the next field
func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
    let next = textField.tag + 1
    // Find next responder
    if let nextField = view.viewWithTag(next) as? UITextField {
        nextField.becomeFirstResponder()
    } else {
        // Not found, so remove keyboard.
        textField.resignFirstResponder()
        //do the final action to login
        loginAction()
    }
    return false
}
stevenpcurtis
  • 1,907
  • 3
  • 21
  • 47
0

Since iOS 9, you can connect the “Primary Action Triggered” event of your UITextField to an action method. So you can connect each text field's “Primary Action Triggered” event to a separate action method to give the text fields different actions.

See this answer for a demo: https://stackoverflow.com/a/26288292/77567

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
0

I realized I was really over thinking it.. Took another look and saw what I was trying to do was a lot easier then I thought.. Thanks everybody who helped! (just an example below)

 func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    if field1.resignFirstResponder() {
        print("yellow")
    }
    if field2.resignFirstResponder() {
        print("blue")
    }
    return (true)
}
stevenpcurtis
  • 1,907
  • 3
  • 21
  • 47
Sam
  • 3
  • 5