UIPickerView显示空灰框Swift

时间:2015-08-05 10:40:07

标签: ios swift uipickerview

我还没有很多代码,所以我不妨在这里复制它。

select

打印陈述是正确的。对于selectclass 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 } } ,它会打印正确的字符串。

numberOfRows...没有打印件,因为我无法选择一行。

这就是我得到的:

enter image description here

您可以在后台忽略Google地图,这不应该影响Picker View并且只是在StoryBoard中设置。

单击textField但不显示任何内容时会出现灰色窗口。但印刷声明另有说法。

有谁知道为什么会这样?

2 个答案:

答案 0 :(得分:3)

只需在viewDidLoad方法中添加此行:

buildPickers.dataSource = self

您的代码将是:

override func viewDidLoad() {
    super.viewDidLoad()

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

它会显示您的数据。

<强>更新

它没有显示,因为您将其隐藏在viewDidLoad

只需从代码中删除此行:

buildPickers.hidden = true

这是您的工作代码:

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

答案 1 :(得分:0)

是否在文本字段中设置了委托?您是否收到了用于取消隐藏选择器的回调?