UIAlertController操作表中的UIPickerView不显示任何数据

时间:2015-09-21 22:57:05

标签: ios swift uipickerview uialertcontroller

我尝试以编程方式创建UIPickerView并在操作表中显示它的行。然而,似乎没有任何工作。以下是类似的非工作代码的简化示例:

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

    let array = ["one","two","three","four","five"]
    var controller: UIAlertController?
    var pickerView = UIPickerView(frame: CGRectMake(0, 15, 304, 0))

    @IBOutlet weak var button: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        controller = UIAlertController(title: "Choose Category", message: "\n\n\n\n\n\n\n\n", preferredStyle: .ActionSheet)
        let doneAction = UIAlertAction(title: "Done", style: .Default, handler: nil)
        controller!.addAction(doneAction)

        pickerView.delegate = self
        pickerView.dataSource = self
    }

   @IBAction func buttonPressed(sender: AnyObject) {
        controller!.view.addSubview(self.pickerView)
        self.presentViewController(controller!, animated: true, completion: nil)
    }

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

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return array.count
    }

    func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 50.0
    }

    func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
        let view = UIView(frame: CGRectMake(0, 0, 100, 70))

        let label = UILabel(frame: CGRectMake(50, 5, 100, 60))
        label.text = array[row] as String

        view.insertSubview(label, atIndex: 1)

        return view
    }
}

按下按钮时,这是输出:

Screenshot of a UIAlertController being displayed on an iPhone 4S

如您所见,操作表未填充数组值。在我升级到iOS 9和Swift 2.0之前,这一切都正常工作。我已使用print确保调用pickerView方法,甚至使用print检查labelview的值,它们是numberOfComponentsInPickerView: numberOfComponentsInPickerView: rowHeightForComponent: rowHeightForComponent: numberOfRowsInComponent: numberOfRowsInComponent: numberOfRowsInComponent: numberOfRowsInComponent: numberOfRowsInComponent: numberOfRowsInComponent: numberOfRowsInComponent: numberOfRowsInComponent: numberOfRowsInComponent: numberOfComponentsInPickerView: numberOfRowsInComponent: viewForRow: viewForRow: numberOfComponentsInPickerView: rowHeightForComponent: rowHeightForComponent: numberOfRowsInComponent: numberOfRowsInComponent: numberOfRowsInComponent: numberOfRowsInComponent: numberOfRowsInComponent: numberOfRowsInComponent: numberOfRowsInComponent: numberOfRowsInComponent: numberOfRowsInComponent: viewForRow: viewForRow: viewForRow: viewForRow: viewForRow: 那里。任何人都可以指导我一个方向或解释是什么导致这不显示?

另外:另一个时髦的事情是如何调用这些方法。如果我按下按钮一次,这就是发生的每个方法调用的顺序:

{{1}}

1 个答案:

答案 0 :(得分:2)

您不应该修改UIAlertController的视图层次结构。即使你能够以这种方式工作,你也有可能在任何时候有更新的时候让苹果破坏它,并且你必须不断寻找新的解决方法才能使它工作。您可能需要考虑将选择器放在视图控制器中并将其显示为UIModalPresentationPopover。 如果您将其与响应者子类(如文本字段或您自定义的子类)一起使用,您还可以创建一个看起来像带有选择器的操作表的视图,并将其设置为响应者的inputView