Ios 在ViewdidLoad之外添加UITextField
我似乎不知道如何在viewdidload方法之外添加TextField 我有一个函数,它检查UIPickerView中的一个选择是否为Europe,如果是,它需要添加一个新的TextField,如果不是,并且TextField存在,它应该删除它 这是我的密码:Ios 在ViewdidLoad之外添加UITextField,ios,xcode,swift,storyboard,uitextfield,Ios,Xcode,Swift,Storyboard,Uitextfield,我似乎不知道如何在viewdidload方法之外添加TextField 我有一个函数,它检查UIPickerView中的一个选择是否为Europe,如果是,它需要添加一个新的TextField,如果不是,并且TextField存在,它应该删除它 这是我的密码: func updateLabel(){ switch country{ case "Internationaal": tailleSize = taille.internati
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)
}
}
每次用户按下按钮或从UIPickerView中选择项目时,都会运行updateLabel
我该怎么做
我试图在一个变量中获取主viewcontroller并将其添加到该变量中,但失败了。我认为单独定义UITextField更有意义。将其添加到viewDidLoad上隐藏的视图中,这将使switch语句更清晰,每次都可以重新使用而不是重新创建textfield 视图控制器
您还可以利用enabled属性来启用和禁用textfield,而不是使其不可见。myTextField.enabled=false以正常方式将文本字段添加到您想要的故事板位置,并将其与代码连接
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)
}
}
为什么不在Europe案例中添加textField,您还可以检查textField是否存在,如果不存在,则添加itAm I Imaging things,或者您正在创建UITextField,但没有将其作为子视图添加到页面的UIView?如果不这样做,您只需创建一个UITextField,然后在不将其添加到页面的情况下将其释放。启用将使其可见并显示为灰色。隐藏将使其不可见。您的解决方案与@Wezly几乎相同,但现在我不必担心以编程方式添加约束。非常感谢。
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)
}
}