在Pickerview(Swift 3,Xcode)中进行(第一个)行不可选择



我正在尝试使我的选择器视图的第一行无法选择,但它不起作用。我已经尝试了以下选项,但是两个都给出了一些错误或用于以前的swift版本
- 禁用Uipickerview中的特定行值
- (ios(如何在Uipickerview中获得第一个值?

我的代码:

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"]

我想在两个Pickerviews中使"选择"不可选择。另外,如果用户不更改pickerview选项(即使选择"选择"(,我也想发送一个错误,即使它是不可选择的(。

按钮代码继续:

@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))
}
}

我相信,在您的情况下以下将有效:

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.
}

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

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中选择任何行时,字符串存储在字符串中,您还可以看到选择的行号。

您还可以添加一个按钮,以便如果字符串具有" - 选择 - ",则在按钮操作中,它将显示一个警报。或者将行值存储在整数变量和按钮操作中,如果整数变量== 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
  }
}

最新更新