Skip to content

Text field return bug fix #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Source/JHTAlertController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ public class JHTAlertController: UIViewController, UIViewControllerTransitioning
/// - Returns: should return value
public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
textField.nextTextFieldField()?.becomeFirstResponder()
return true
}

Expand All @@ -606,3 +607,37 @@ public class JHTAlertController: UIViewController, UIViewControllerTransitioning
return JHTAlertAnimation(isPresenting: false)
}
}

extension UITextField{
// Author of extension: Imbru from StackOverflow
// Allows keyboard next/done buttons to function correctly with more than one field input.
func nextTextFieldField() -> UITextField?{
//field to return
var returnField : UITextField?
if self.superview != nil{
//for each view in superview
for (_, view) in self.superview!.subviews.enumerated(){
//if subview is a text's field
if view is UITextField {
//cast curent view as text field
let currentTextField = view as! UITextField
//if text field is after the current one
if currentTextField.frame.origin.y > self.frame.origin.y{
//if there is no text field to return already
if returnField == nil {
//set as default return
returnField = currentTextField
}
//else if this this less far than the other
else if currentTextField.frame.origin.y < returnField!.frame.origin.y{
//this is the field to return
returnField = currentTextField
}
}
}
}
}
//end of the mdethod
return returnField
}
}