在pickerview中使(第一)行无法选择(Swift 3,Xcode)

时间:2017-05-04 08:14:26

标签: swift uipickerview

我试图让我的选择器视图的第一行无法选择,但它不起作用。我已经尝试了以下选项,但它们都有一些错误或适用于以前的Swift版本
- Disable particular row value in UIPickerView
- (iOS) How to make first value in UIPickerView unselectable?

我的代码:

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if (pickerView.tag == 1){
        return list1.count
    } else {
        return list2.count
    }
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if (pickerView.tag == 1){
        return "\(list1[row])"
    } else {
        return "\(list2[row])"
    }
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if pickerView.tag == 1 {
        frequency = list1[row]
    } else {
        period = list2[row]
    }
}

我的阵列:

list1 = ["Select", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"]
list2 = ["Select", "days", "weeks"]

我想做"选择"在两个挑选者视图中都无法选择。如果用户没有更改选择器视图选项(尝试继续"选择"选择,即使它是不可选的),我也想发送错误。

按钮代码继续:

@IBAction func addActivity(_ sender: Any) {
if (input.text != "" && pickerString != "Select") {
    nameActivity = input.text!
    list.append(nameActivity)
    input.text = ""
} else if (input.text == "") {
    let alertController = UIAlertController(title: "Error", message:
        "Please give the activity a name!", preferredStyle: UIAlertControllerStyle.alert)
    alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default,handler: nil))
    self.present(alertController, animated: true, completion: nil)
} else if pickerString == "Select" {
    let alertController = UIAlertController(title: "Error", message:
        "Select something", preferredStyle: UIAlertControllerStyle.alert)
    alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default,handler: nil))
}
}

2 个答案:

答案 0 :(得分:0)

你可以做一件事。让你的第一个元素是“ - 选择 - ”。现在创建一个类型的变量,将选择器视图行值存储在此字符串中。如果所选行等于“--Select--”,则创建警报并通知用户选择您的选项。

OR

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    // Check row

    print(row)

    if(row == 0)
    {
       //Create an alert
    }
    else
    {
       // IMPLEMENT_CODE
    }

}

更新1:

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate 
{  

   @IBOutlet var pickerView: [UIPickerView]!

   var pickerString:String = "--Select--"

   var array:[String] = ["--Select--","Apple","Ball","Cat","Dog"]



// number of rows or number of components

func numberOfComponents(in pickerView: UIPickerView) -> Int {

    return 1
}

// number of rows that we have to set

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

    return array.count
}

// set title in picker view according to the elements

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

    return array[row]
}

// when an element is selected, store in a string

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    print(row)

    pickerString = array[row]

    print(pickerString)

}

这里当你在pickerView中选择任何一行时,字符串存储在字符串中,你也可以看到所选的行号。

您还可以添加一个按钮,这样当按钮操作时,如果字符串中包含“--Select--”,它将显示警告。 要么 将行值存储在整数变量和按钮操作中,如果整数变量== 0,则创建警报。

更新2:

@IBAction func addActivity(_ sender: Any) {
if (input.text == "") {
let alertController = UIAlertController(title: "Error", message:
    "Please give the activity a name!",preferredStyle:UIAlertControllerStyle.alert)

alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default,handler: nil))

self.present(alertController, animated: true, completion: nil)
} 
else 
if pickerString == "Select" {
let alertController = UIAlertController(title: "Error", message:
    "Select something", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default,handler: nil))
}
else
{
nameActivity = input.text!
list.append(nameActivity)
input.text = ""
}
}

更新3:

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate, UITextViewDelegate {

@IBOutlet var pickerView: [UIPickerView]!

@IBOutlet var selectAny: UILabel!

var pickerString:String = “—-Select-—“

var array:[String] = ["—-Select-—“,”Apple”,”Ball”,”Cat”,”Dog”]

// number of rows or number of components

func numberOfComponents(in pickerView: UIPickerView) -> Int {

    return 1
}

// number of rows that we have to set

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

    return array.count
}

// set title in picker view according to the elements

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

    return array[row]
}

// when an element is selected, store in a string

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    print(row)

    pickerString = array[row]

    print(pickerString)

}

// Button action

@IBAction func buttonAction(_ sender: UIButton) {

    print(pickerString)

    print(length!)

    if(pickerString == “--Select--“){

        let alert = UIAlertController(title: “Error”, message: "Please select.”, preferredStyle: UIAlertControllerStyle.alert)

        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))

        self.present(alert, animated: true, completion: nil)
    }

    else
            if(pickerString != “—-Select—-“){

                yourCode()
    }
}

func yourCode()
  {
     // do whatever you want
  }
}

答案 1 :(得分:0)

我相信以下内容适用于您的情况:

override open func viewDidLoad() {
    super.viewDidLoad()

    // Set the initial selection of both picker views to the first item, this will prevent the user from proceeding without changing the selections.
    frequencyPicker.selectRow(0, inComponent: 0, animated: false)

    periodPicker.selectRow(0, inComponent: 0, animated: false)
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    var selectedRow = row

    if selectedRow == 0 {

        selectedRow = 1

        pickerView.selectRow(selectedRow, inComponent: component, animated: false)
    }

    if pickerView.tag == 1 {
        frequency = list1[selectedRow]
    } else {
        period = list2[selectedRow]
    }
}

在您的继续操作中,您可以实现以下内容:

func continue() {

    if frequencyPicker.selectedRow(inComponent: 0) == 0 || periodPicker.selectedRow(inComponent: 0) == 0 {

        // Alert the user that he needs to pick the correct values.
        return
    }

    // Perform some action to proceed in the application.
}