简体   繁体   中英

Adding UITextField outside ViewdidLoad

I cannot seem to figure out how to add a TextField outside the viewdidload method.

I have a function which checks if a selection (in UIPickerView ) is Europe, if it is, it needs to add a new TextField , if it isn't (and the TextField exists) it should remove it.

here is my code:

func updateLabel(){
   switch country{
            case "Internationaal":
                tailleSize = taille.international
            case "Europe":
                var myTextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 40.00));
                myTextField.backgroundColor = UIColor.grayColor();
                myTextField.placeholder="  Enter here";
                //myTextField.text = "    Enter here";
                myTextField.borderStyle = UITextBorderStyle.Line;
                myTextField.secureTextEntry=true;
                tailleSize = String(taille.europe)
           default: 
                tailleSize = String(taille.europe)
   }
}

updateLabel is run everytime a user pressed a button , or selects an item from the UIPickerView .

How can i do this?

I tried to get the main viewcontroller in a variable and add it there, but that was a failure.

I think it would make more sense to define your UITextField separately adding it to the view hidden on viewDidLoad , it would make for a cleaner switch statement and your textfield can be re-used instead of re-created each time.

ViewController

override func viewDidLoad() {
    super.viewDidLoad()
    addTextFieldToView()
}

var myTextField:UITextField!

func addTextFieldToView() {
    myTextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 40.00))
    myTextField.backgroundColor = UIColor.grayColor()
    myTextField.placeholder = "Enter here"
    myTextField.borderStyle = UITextBorderStyle.Line
    myTextField.secureTextEntry = true
    myTextField.hidden = true
    self.view.addSubview(myTextField)
}

func updateLabel(){
    switch country{
        case "International":
            tailleSize = taille.international
            myTextField.hidden = true
        case "Europe":
            tailleSize = String(taille.europe)
            myTextField.hidden = false
       default: 
            tailleSize = String(taille.europe)
            myTextField.hidden = true
    }
}

You could also utilize the enabled property to enable and disable the textfield instead of making it invisible. myTextField.enabled = false

Add the TextField to your Storyboard at the place you want in the "normal" way and connect it with your code.

override func viewDidLoad() {
        super.viewDidLoad()
        myTextField.hidden = true; //TextField disappears
        //You also can set the textField in Interface Builder to hidden.
    }

func updateLabel(){
   switch country{
            case "Internationaal":
                tailleSize = taille.international
            case "Europe":
                myTextField.hidden = false; //TextField appears
                myTextField.backgroundColor = UIColor.grayColor();
                myTextField.placeholder="  Enter here";
                //myTextField.text = "    Enter here";
                myTextField.borderStyle = UITextBorderStyle.Line;
                myTextField.secureTextEntry=true;
                tailleSize = String(taille.europe)
           default: 
                tailleSize = String(taille.europe)
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM