简体   繁体   English

UIPickerView显示空的灰色框Swift

[英]UIPickerView showing empty grey box Swift

I haven't a whole lot of code, so I might as well copy it here. 我没有很多代码,所以我最好在这里复制它。

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{

    var buildings = ["BankBuilding", "Cinema" , "CornerShop", "Greg's House"]

    @IBOutlet weak var buildText: UITextField!

    var buildPickers:UIPickerView = UIPickerView()

    override func viewDidLoad() {
        super.viewDidLoad()

        buildPickers = UIPickerView()
        buildPickers.delegate = self
        buildPickers.hidden = true;
        buildText.inputView = buildPickers
        buildText.text = buildings[0]        
    }

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

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
        println("Count: \(buildings.count)")
        return buildings.count
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        println("creating title: \(buildings[row])")
        return buildings[row]
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
        println("Selected: \(buildings[row])")
        buildText.text = buildings[row]
        buildPickers.hidden = true;
    }

    func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
        buildPickers.hidden = false
        return false
    }
}

The print statements are correct. 打印语句正确。 For numberOfRows... and titleForRow it is printing the correct Strings. 对于numberOfRows...titleForRow它正在打印正确的字符串。

But there is no prints for didSelectRow because, well, I can't select a row. 但是didSelectRow没有打印,因为,我不能选择一行。

This is what I get: 这是我得到的:

在此处输入图片说明

You can ignore the Google Map in the background, that shouldn't interfere with the Picker View and is just set up in the StoryBoard. 您可以在后台忽略Google Map,它不会干扰Picker View,而只是在StoryBoard中进行设置。

The Grey window appears when I click on the textField but never shows any content. 当我单击textField时,出现“灰色”窗口,但从不显示任何内容。 But the print statements say otherwise. 但是印刷声明却相反。

Does anyone know why this is the case? 有谁知道为什么会这样吗?

Just add this line in your viewDidLoad method: 只需在您的viewDidLoad方法中添加以下行:

buildPickers.dataSource = self

And your code will be: 您的代码将是:

override func viewDidLoad() {
    super.viewDidLoad()

    buildPickers = UIPickerView()
    buildPickers.delegate = self
    buildPickers.dataSource = self
    buildPickers.hidden = true;
    buildText.inputView = buildPickers
    buildText.text = buildings[0]        
}

And it will show your data. 它将显示您的数据。

UPDATE: 更新:

It is not showing because you set it hidden in your viewDidLoad . 由于您将其设置为隐藏在viewDidLoad ,因此未显示。

Just remove this line from your code: 只需从代码中删除此行:

buildPickers.hidden = true

Here is your working code: 这是您的工作代码:

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate{

    var buildings = ["BankBuilding", "Cinema" , "CornerShop", "Greg's House"]

    @IBOutlet weak var buildText: UITextField!

    var buildPickers:UIPickerView = UIPickerView()

    override func viewDidLoad() {
        super.viewDidLoad()

        buildPickers = UIPickerView()
        buildPickers.delegate = self
        buildPickers.hidden = true
        buildText.delegate = self        //set delegate for textField
        buildText.inputView = buildPickers
        buildText.text = buildings[0]
    }

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

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
        println("Count: \(buildings.count)")
        return buildings.count
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        println("creating title: \(buildings[row])")
        return buildings[row]
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
        println("Selected: \(buildings[row])")
        buildText.text = buildings[row]
        buildPickers.hidden = true;
    }


    //this method will call when you click on textField
    func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
        buildPickers.hidden = false
        return true
    }
}

Is the delegate set on the text field? 是否在文本字段上设置了代表? Are you getting the callback which you use to un-hide the picker? 您是否获得用于取消隐藏选择器的回调?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM