UIPickerView 选择将 nil 值发送到新的 ViewController

Posted

技术标签:

【中文标题】UIPickerView 选择将 nil 值发送到新的 ViewController【英文标题】:UIPickerView selection sends nil value to new ViewController 【发布时间】:2018-10-04 05:54:21 【问题描述】:

我正在尝试将UIPickerView 的选择作为字符串传递给新的ViewController,不幸的是,当我这样做时,它返回了一个nil 值。我一直在关注一些不同的教程,但我似乎无法让它工作。任何建议将不胜感激。

class menu: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource 

    @IBOutlet weak var pickerView: UIPickerView!
    @IBOutlet weak var newsButton: UIButton!

    var pickerDataSource = ["Canterbury-Bankstown Torch", "Auburn Review", "Inner West Times", "Seniors "];
    var chosenState = String()

    override func viewDidLoad() 
        super.viewDidLoad()
        pickerView.delegate = self
        pickerView.dataSource = self
    

    func numberOfComponents(in pickerView: UIPickerView) -> Int 
        return 1
    

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
        return pickerDataSource.count;
    

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
        return pickerDataSource[row] as String
    

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
        chosenState = pickerDataSource[row]
    

    func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
        if segue.identifier == "customSegue" 
            if let secondViewController = segue.destination as? ViewController 
                secondViewController.chosenState = chosenState
            
        
    

    @IBAction func newsButton(_ sender: Any) 
        self.performSegue(withIdentifier: "customSegue", sender: nil)
    

新的 viewController 包含:

    var chosenState: String?

    override func viewDidLoad() 
        super.viewDidLoad()

        finalPublication?.text = chosenState
        print(chosenState) //Just so I can see if it works

【问题讨论】:

尝试调试并检查您是否在函数中获得正确的值:pickerView:didSelectRow:inComponent & prepareForSegue。 检查你的执行是否到达secondViewController.chosenState = chosenState 您是在选择器视图中选择一个值吗? didSelectRow 仅在您将选取器视图移动到不同行时调用。 如果我硬编码 secondViewController.chosenState = "test" 例如,我仍然会收到一个 nil 值,但是当我打印 didSelectRow 时,它会返回我选择的内容,但仍未将其传递给新的 ViewController。 它在哪里返回nil 【参考方案1】:

你用错了方法func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)

你应该使用这个:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
    if segue.identifier == "customSegue" 
        if let secondViewController = segue.destination as? ViewController 
            secondViewController.chosenState = chosenState
        
    

【讨论】:

嗨 Satish,在 UIPickerView 中选择一个项目后,您的建议非常有效(谢谢!),任何关于如何在加载时也传递初始选择的建议。 (例如默认选择)? @user2869163 读取 cmets。已经解释过了。【参考方案2】:

您需要覆盖prepare for segue,以便prepareForSegue 调用并且您在第二个控制器中获得您选择的值。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
    if segue.identifier == "customSegue" 
        if let secondViewController = segue.destination as? ViewController1 
            secondViewController.chosenState = chosenState
        
    

【讨论】:

以上是关于UIPickerView 选择将 nil 值发送到新的 ViewController的主要内容,如果未能解决你的问题,请参考以下文章

如何将 UIPickerView 选定的行发送到另一个 ViewController

未选中的 UIPickerView 值

UIPickerView:在展开可选值时意外发现 nil

将 UIPickerView 选择保存到 NSUserDefaults swift

UIPickerView 行选择

UIPickerView 作为按钮操作并根据选择器的选定值设置文本字段的文本