Emma Campbell
Emma Campbell

Reputation: 23

How to call a function related to a specific UITextField?

I'm very new to Swift and Xcode so please bear with me.

I have a UIViewController for a Create Account screen which has 4 UITextFields. The first 3 can be edited fine when the keyboard appears but the last one needs the UIView to be pushed so it is visible.

I have installed the Cocoapod IHKeyboardAvoiding which is working great, only when I tap on the top three UITextfields the screen also pushes up. The pseudo code for this is: 'When confirm password textfield is tapped the keyboard push should occur. When all other textfields are tapped, no keyboard push should occur.'

I'm unsure how to implement the pseudo code into actual swift code. I've created an IBAction for the confirm password textfield and implemented the keyboard push code within it. This works but as soon as the confirm password textfield is tapped and then another textfield is tapped the keyboard push gets applied to all the textfields. I'm thinking that the function needs to be called within the viewDidLoad but you can't have an IBAction function within viewDidLoad.

What would be the best way to call the function?

import UIKit
import IHKeyboardAvoiding

class LoginViewController: UIViewController {

@IBOutlet var avoidingView: UIView!

@IBAction func confirmPassword(_ sender: UITextField) {
      KeyboardAvoiding.avoidingView = self.avoidingView
}


override func viewDidLoad() {
    super.viewDidLoad()

}

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

Upvotes: 0

Views: 1941

Answers (2)

DevKyle
DevKyle

Reputation: 1091

  1. First you want to set your class to conform to UITextFieldDelegate:

So in your case,

class LoginViewController: UIViewController, UITextFieldDelegate {
  1. Then, set the tag as CodeBender describes above. You only have to do this for the 4th textfield that you DO want to move the screen for.

  2. Set all 4 textfields' delegate to self. For example, if your textfield are named one, two, three, four

    one.delegate = self
    two.delegate = self
    three.delegate = self
    four.delegate = self
    //Change the return key to Done so that tapping it dismisses keyboard
    four.returnKeyType = .done
    
  3. Add the following to your class:

    func textFieldDidBeginEditing(_ textField: UITextField) {
    
        if textField.tag == 1 {
    
            UIView.animate(withDuration: 0.3, animations: {
    
                self.view.frame.origin.y -= 224
    
            }) 
    
        }
    }
    
    
    
    func textFieldShouldEndEditing(_ textField: UITextField) {
    
        textField.resignFirstResponder()
    
        if textField.tag == 1 {
    
            UIView.animate(withDuration: 0.3, animations: {
    
                self.view.frame.origin.y += 224
    
            }) 
    
        }
    
    }
    

That should do it, let me know if you run into anything!

Upvotes: 1

CodeBender
CodeBender

Reputation: 36612

Take your password textfield and apply a tag value to it like this:

passwordTextfield.tag = 1

If you have not connected the textfield to your view controller, you can also set the value in your storyboard file:

enter image description here

Then, in your confirmPassword function, do a check against the tag value:

if (sender.tag == 1) { ... }

Upvotes: 0

Related Questions