从UIButton动作更改uitextField placeHolder

我有一个按钮操作,检查两个uitextfield.text值是否为空,这意味着用户没有输入任何内容,如果是这种情况,则我们更改placeHolder文本。

代码段:

    let qArr1 = ["sample1","sample2","sample3"]
    let qArr2 = ["sample4","sample5","sample6"]
    
    var indexA = Int.random(in: 0..<qArr1.count)
    var indexB = Int.random(in: 0..<qArr2.count)
    
    objc
    func submit(_ sender: UIButton) {
       if textFieldA.text.isEmpty || textField2.text.isEmpty {
       // present an alert to the user
       textFieldA.placeHolder = qArr1[indexA]
       textFieldB.placeHolder = qArr2[indexB]
     } 
     // perform actions if textFields have entered values
}

我正在从字符串数组中随机获取placeHolder文本。因此,每次您有空的textFields时,placeHolder都应使用数组中新随机选择的字符串进行更新。

问题placeHolder文本未在相应的textField中更新。

我肯定我错过了一个简单的步骤。

iCMS 回答:从UIButton动作更改uitextField placeHolder

我已经运行了您的代码,存在多个错误,现在我更新了您的代码,如下所示:-

 let qArr1 = ["sample1","sample2","sample3"]
 let qArr2 = ["sample4","sample5","sample6"]

 var indexA = Int()
 var indexB = Int()


@IBAction func submit(_ sender: UIButton) {

   self.indexA = Int.random(in: 0..<qArr1.count)
   self.indexB = Int.random(in: 0..<qArr2.count)

   if textFieldA.text!.isEmpty || textField2.text!.isEmpty {
        textFieldA.placeholder = qArr1[indexA]
        textFieldB.placeholder = qArr2[indexB]
    } 
   // perform actions if textFields have entered values
}
本文链接:https://www.f2er.com/1848174.html

大家都在问