How to Move Cursor from One Text Field to Another Automatically in Swift iOS Programmatically

How to move cursor from one text field to another automatically in swift ios programmatically?

Set textField delegate and add target:

override func viewDidLoad() {
super.viewDidLoad()

first.delegate = self
second.delegate = self
third.delegate = self
fourth.delegate = self

first.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
second.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
third.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
fourth.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
}

Now when text changes change textField

func textFieldDidChange(textField: UITextField){

let text = textField.text

if text?.utf16.count >= 1{
switch textField{
case first:
second.becomeFirstResponder()
case second:
third.becomeFirstResponder()
case third:
fourth.becomeFirstResponder()
case fourth:
fourth.resignFirstResponder()
default:
break
}
}else{

}
}

And lastly when user start editing clear textField

extension ViewController: UITextFieldDelegate{
func textFieldDidBeginEditing(textField: UITextField) {
textField.text = ""
}
}

How to move cursor from one text field to another automatically when the user clicks the Delete Button in swift ios programmatically?

You can use this:

func textFieldDidChange(textField: UITextField) {
let text = textField.text!
if text.utf16.count == 0 {
switch textField {
case textField2:
textField1.becomeFirstResponder()
case textField3:
textField2.becomeFirstResponder()
case textField4:
textField3.becomeFirstResponder()
case textField5:
textField4.becomeFirstResponder()
case textField6:
textField5.becomeFirstResponder()
default:
break
}
} else if text.utf16.count == 2 {
let indexStartOfText = text.index(text.startIndex, offsetBy: 1)
textField.text = String(text[..<indexStartOfText])
let tempStr = String(text[indexStartOfText])
switch textField {
case textField1:
textField1.backgroundColor = UIColor.black
textField1.textColor = .white
textField2.becomeFirstResponder()
textField2.backgroundColor = UIColor.black
textField2.textColor = .white
textField2.text = tempStr
case textField2:
textField3.becomeFirstResponder()
textField3.backgroundColor = UIColor.black
textField3.textColor = .white
textField3.text = tempStr
case textField3:
textField4.becomeFirstResponder()
textField4.backgroundColor = UIColor.black
textField4.textColor = .white
textField4.text = tempStr
case textField4:
textField5.becomeFirstResponder()
textField5.backgroundColor = UIColor.black
textField5.textColor = .white
textField5.text = tempStr
case textField5:
textField6.becomeFirstResponder()
textField6.backgroundColor = UIColor.black
textField6.textColor = .white
textField6.text = tempStr
case textField6:
textField6.resignFirstResponder()
default:
break
}
}
}

In this code you will never focus on the 1 letter of UITextFields instead you focus on 2 letters, if the sender.text.count equals to 2 you set the second number for the next UITextField, and If sender.text.count equals to zero it means user has deleted something (because we are in Editing Changed event)and we should backward the cursor.

And do everything you want for UI in the first statement of if.

It's a little messy but it works.

How do I programatically move the cursor from one NSTextField to another after a character count has been reached?

The answer to part of this this question was posted here by @cheesey

here is the complete code to create a window that takes the license key for a product from a user (This is using swift 4).

First set the Text Fields as the delegates and first responders in the viewDidLoad Function and then change the first responder once the string limit is hit

class CommercialActivationView: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
@IBOutlet weak var firsttextfield: NSTextField!
@IBOutlet weak var secondtextfield: NSTextField!
@IBOutlet weak var thirdtextfield: NSTextField!

firsttextfield.window?.makeFirstResponder(firsttextfield)
firsttextfield.delegate = self
}

func makeFirstResponder() {

if firsttextfield.stringValue.count == 5 {
firsttextfield.window?.makeFirstResponder(secondtextfield)
}
if secondtextfield.stringValue.count == 5 {
secondtextfield.window?.makeFirstResponder(thirdtextfield)
}
}
}

Now to create the extension that creates the character limit or the text field every time the user edits the TextField (Here i'm limiting the number of characters per text field to 5).

extension CommercialActivationView: NSTextFieldDelegate {
func controlTextDidChange(_ obj: Notification) {

let object = obj.object as! NSTextField
if object.stringValue.count > 5{
object.stringValue = String(object.stringValue.dropLast())
makeFirstResponder()
}
}

This works such that once 5 characters are reached in 1 TextField it switches to the next one automatically. Also the code I've posted is for 3 TextFields more text fields can be add if needed.

Move cursor/focus on other textfield

To move focus to another responder you can use[textField2 becomeFirstResponder];. I can also suggest a IQKeyboardManager library which adds a similar toolbar automatically.

How to move the cursor to next UITextField programmatically?

Catch the delegate that informs you your textfield has been changed. Check if the new length is 1. If it is, call becomeFirstResponder on the next field.

UITextField, automatically move to next after 1 character

I arrived at a solution by modifying some code I found here:
http://www.thepensiveprogrammer.com/2010/03/customizing-uitextfield-formatting-for.html

First set the your view controller to be the delegate of the textfields.

Then do something like this:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
BOOL shouldProcess = NO; //default to reject
BOOL shouldMoveToNextField = NO; //default to remaining on the current field

int insertStringLength = [string length];
if(insertStringLength == 0){ //backspace
shouldProcess = YES; //Process if the backspace character was pressed
}
else {
if([[textField text] length] == 0) {
shouldProcess = YES; //Process if there is only 1 character right now
}
}

//here we deal with the UITextField on our own
if(shouldProcess){
//grab a mutable copy of what's currently in the UITextField
NSMutableString* mstring = [[textField text] mutableCopy];
if([mstring length] == 0){
//nothing in the field yet so append the replacement string
[mstring appendString:string];

shouldMoveToNextField = YES;
}
else{
//adding a char or deleting?
if(insertStringLength > 0){
[mstring insertString:string atIndex:range.location];
}
else {
//delete case - the length of replacement string is zero for a delete
[mstring deleteCharactersInRange:range];
}
}

//set the text now
[textField setText:mstring];

[mstring release];

if (shouldMoveToNextField) {
//
//MOVE TO NEXT INPUT FIELD HERE
//
}
}

//always return no since we are manually changing the text field
return NO;
}


Related Topics



Leave a reply



Submit