Search code examples
iosswiftiboutletcollection

Register form with outlet collection and tags


I am new to Swift Language. I have knowledge in Web Development, but I need your help regarding this.

I need to create a register page with outlet collection. I have 4 text fields and have assigned tags to each of them. As I am going towards moving the cursor, from one textfield to another using beingfirstresponder(), it is not working and when I am printing tags like what I have assigned a tag to it's textfield, it is not always printing the same.

It is coming in a random order. Here is the code.

import UIKit

class ViewController: UIViewController,UITextFieldDelegate {

@IBOutlet var registerTF: [UITextField]!


@IBAction func registerTFTapped(_ sender: UITextField) {

   let tag = sender.tag
   print(tag)
    for tf in registerTF
    {
     if tf.tag == tag
     {

        func textFieldShouldReturn( _ textField: UITextField) -> Bool {

            nextTextFieldToFirstResponder(textField: textField)

            return true;
        }

        func nextTextFieldToFirstResponder(textField: UITextField) {

            if tf.tag == 0 {

                self.becomeFirstResponder()

            }
            else if tf.tag == 1 {

                self.becomeFirstResponder()

            }
            else if tf.tag == 2 {

                self.becomeFirstResponder()

            }
            else if tf.tag == 3 {

                self.resignFirstResponder()

            }

        }

        }
    }
}

//LifeCycle-Starts

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func viewWillAppear(_ animated: Bool) {
    //#
}

override func viewDidAppear(_ animated: Bool) {
    //#
}

override func viewWillDisappear(_ animated: Bool) {
    //#
}

override func viewDidDisappear(_ animated: Bool) {
    //#
}

//LifeCycle-Ends

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

Solution

  • This only will work if the order in the collection of TextFields is equal to the tags order

    import UIKit
    
    class ViewController: UIViewController,UITextFieldDelegate {
    
        @IBOutlet var registerTF: [UITextField]!
    
        func nextTag(currentTag:Int)->Int{
    
            if(currentTag < registerTF!.count - 1){
                return currentTag + 1
            }
            return -1
        }
    
        func nextTextFieldToFirstResponder(textField: UITextField) {
    
            let nextTag = self.nextTag(currentTag: textField.tag)
    
            if(nextTag >= 0){
                self.registerTF![nextTag].becomeFirstResponder()
            }else{
                textField.resignFirstResponder()
            }
        }
    
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            self.nextTextFieldToFirstResponder(textField: textField)
            return true
        }
    
        //LifeCycle-Starts
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
    
            for textField in registerTF {
                textField.delegate = self
            }
        }
    
        override func viewWillAppear(_ animated: Bool) {
            //#
        }
    
        override func viewDidAppear(_ animated: Bool) {
            //#
        }
    
        override func viewWillDisappear(_ animated: Bool) {
            //#
        }
    
        override func viewDidDisappear(_ animated: Bool) {
            //#
        }
    
        //LifeCycle-Ends
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }