微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

检查 textView 选择器视图是否包含某个字符串

如何解决检查 textView 选择器视图是否包含某个字符串

我有一个允许人们发帖的应用。我正在尝试检查我的 textView 选择器是否包含某个文本,如果它包含一个警报应该显示告诉他们再次选择。

下图

image

如果文本包含“工作类别”,我希望显示一个提醒,提示选择一个类别。

该字段是嵌入在 textView 中的 pickerView。这是我的设置方法

var data = ["Assembly","Auto Care","Electronic Help","Item Delivery","Handyman","House Chores","Junk Removal","Lawn & Yard Care","Moving","Painting","Pet Care","Seasonal Work","Other"]
var picker = UIPickerView()
func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView,numberOfRowsInComponent component: Int) -> Int {
    return data.count
}
func pickerView(_ pickerView: UIPickerView,didSelectRow row: Int,inComponent component: Int) {
    JobCategory.text = data[row]
}
func pickerView(_ pickerView: UIPickerView,titleForRow row: Int,forComponent component: Int) -> String? {
    return data[row]
}

let JobCategory: UITextField = {
    let e = UITextField()
    
    let attributedplaceholder = NSAttributedString(string: "Job Category",attributes:
        [NSAttributedString.Key.foregroundColor : GREEN_Theme])
    e.setLeftPaddingPoints(6)
    e.textColor = GREEN_Theme
    e.attributedplaceholder = attributedplaceholder
    e.setBottomBorder(backGroundColor: .white,borderColor: GREEN_Theme)
    
    
    return e
}()

我试图显示警报的位置在这里

if self.JobCategory.text == "Job Category" {

          let alert = UIAlertController(title: "Hold up!",message:" Choose a job category. ",preferredStyle: UIAlertController.Style.alert)


          let continueButton = UIAlertAction(title: "Got it!",style: .default,handler: {(_ action: UIAlertAction) -> Void in

             })

          continueButton.setValue(GREEN_Theme,forKey: "titleTextColor")
          alert.addAction(continueButton)
          self.present(alert,animated: true,completion: nil)

解决方法

 func pickerView(_ pickerView: UIPickerView,didSelectRow row: Int,inComponent component: Int) {
    JobCategory.text = data[row]
    if self.JobCategory.text == "Job Category" {
        // write alert code here
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。