简体   繁体   English

在ViewdidLoad之外添加UITextField

[英]Adding UITextField outside ViewdidLoad

I cannot seem to figure out how to add a TextField outside the viewdidload method. 我似乎无法弄清楚如何在viewdidload方法之外添加TextField

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. 我有一个function ,用于检查某个选择(在UIPickerView )是否为欧洲,如果是,它需要添加一个新的TextField ,如果不是(并且该TextField存在),则应将其删除。

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 . 每当用户按下button或从UIPickerView选择一项时,都会运行updateLabel

How can i do this? 我怎样才能做到这一点?

I tried to get the main viewcontroller in a variable and add it there, but that was a failure. 我试图在变量中获取主viewcontroller并将其添加到那里,但这是失败的。

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. 我认为定义您的UITextField单独将其添加到hiddenviewDidLoad上的视图中会更有意义,这将使switch语句更整洁,并且您的textfield可以重复使用,而不是每次都重新创建。

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. 您也可以利用enabled属性来启用和禁用文本字段,而不是使其不可见。 myTextField.enabled = false

Add the TextField to your Storyboard at the place you want in the "normal" way and connect it with your code. 以“常规”方式将TextField添加到您的Storyboard中所需的位置,并将其与代码连接。

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)
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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