在pickerview(Swift 3,Xcode)中使(第一)行不可选择

Posted

技术标签:

【中文标题】在pickerview(Swift 3,Xcode)中使(第一)行不可选择【英文标题】:Make (first) row unselectable in pickerview (Swift 3, Xcode) 【发布时间】:2017-05-04 08:14:25 【问题描述】:

我正在尝试使我的选择器视图的第一行无法选择,但它不起作用。我尝试了以下选项,但它们都给出了一些错误,或者都适用于以前的 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))


【问题讨论】:

你不能像你提供的链接那样做吗? didSelectRow 方法中的 row == 0 ?这有什么问题? If pickerView.selectedrowincomponent != 0 //随心所欲 @GrzegorzKrukowski 然后我应该将if (row == 0) [pickerView selectRow:row+1 inComponent:component animated:YES]; 放在已经定义的“didSelectRow”中吗?因为这给了我一些错误,所以它想放置几个逗号,最后它说Expected expression in container literal 【参考方案1】:

我相信以下方法适用于您的情况:

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.

【讨论】:

目前适用于两个pickerviews!只是不能彻底解决问题。如果用户使用 pickerviews 加载屏幕,则用户能够在呈现给他们时离开选择器,并且如果用户没有更改它们并继续,Xcode 不会看到实际选择了一行并提供了选择器查看一个空变量。 仍然是相同的..在两种情况下,拾取器视图从第一行更改为第二个 span>时,Pickerview变量是“选择” 【参考方案2】:

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

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
  

【讨论】:

你有一些代码来解释这个吗?因为我真的不明白 我不确定它是否有效。我现在将我的按钮设置为if input.text != "" && pickerString != "--Select") continue else error,但它仍然继续吗?请参阅我更新的按钮代码问题! @GBuis 检查更新 2。 有点工作!因为我有 2 个选择器视图,所以我稍微更改了代码: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 if picker2String == "Select" // same code as before 。如果 input.text = "" 则发送警报,但在选择器视图中的一个或两个选择“选择”时没有警报? :// @deadShot 很高兴为您提供帮助。 :)

以上是关于在pickerview(Swift 3,Xcode)中使(第一)行不可选择的主要内容,如果未能解决你的问题,请参考以下文章

Swift - 无法使 PickerView 库正常工作

如何在 Swift 的 PickerView 中返回两个单独的数组

Swift字典到数组以填充pickerView

如何在 swift 2.2 中更改 pickerview 中的文本/字体大小?

Swift:在国家和城市之间的两个pickerView中解析Json

SWIFT 核心数据加载到pickerview