【问题标题】:check if a textView picker view contains a certain string检查 textView 选择器视图是否包含某个字符串
【发布时间】:2021-09-29 09:31:24
【问题描述】:

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

下图

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

该字段是嵌入在 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)

【问题讨论】:

  • 在pickerview的didselect方法检查条件和显示警报上
  • 我应该评估pickerView而不是textView吗?
  • 是的。你应该评估pickerview而不是textView
  • @RahulPhate 对不起,我仍然遇到问题

标签: ios swift uitextview uipickerview


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    • 2018-05-01
    • 2014-05-07
    • 2019-08-05
    • 2011-03-31
    相关资源
    最近更新 更多