来自segue的UIPickerView项目未加载

时间:2016-07-07 16:37:33

标签: ios swift segue uipickerview

我正在尝试将项目加载到UIPickerView中,这些项目是从上一个视图中的prepareforsegue函数中填充的列表中加载的

class AddItemViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {


    @IBOutlet weak var ItemPickView: UIPickerView!
    @IBOutlet weak var textboxName: UITextField!

    var options: [String]?
    var name: String?

    override func viewDidLoad() {
        super.viewDidLoad()
        print("viewdidload started")
        ItemPickView.delegate = self
        ItemPickView.dataSource = self
        textboxName.inputView = ItemPickView
        print("viewdidload ended")
        print(options)
    }

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

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

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return options![row]
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        textboxName.text = options![row]
    }
}

我的segue代码

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "AddNewItem" {
        print("segue started")
        let controller = segue.destinationViewController as? AddItemViewController
        controller?.options = ["test1","test2","test3"]
        print("segue ended")
    }
 }

似乎UIPickerView是在列表更改之前启动的。对此有何解决方法?

非常感谢

编辑:这是打印状态的日志

segue started
segue ended
viewdidload started
viewdidload ended
nil
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb) 

看来segue在viewdidload之前完成..然后为什么不更新选项列表?

1 个答案:

答案 0 :(得分:0)

一种方法是将文件所有者标记为数据源并委托[在XIB中]。这样就没有必要等待View didload附加必要的数据源和委托。