UIPickerView not showing in popover

1.2k views Asked by At

I have created a UIViewController in my storyboard with a UIPickerView inside. When I perform a manual segue to show this view controller as a popover, the popover is empty.

When debugging I see that my viewDidLoad is called, but the UiPickerViewDataSource and UIPickerViewDelegate functions are not called.

First I thought I had made some mistake with my UIPickerView datasource and delegate. But I ran my application with the UIViewController with the UIPickerView as initial view and the picker was shown as expected.

The picker view code is as follows:

import UIKit

class PickerVC: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

    @IBOutlet weak var pickerView: UIPickerView!

    var pickerDataSource = ["White", "Red", "Green", "Blue"];

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // MARK: - UIPickerViewDataSource

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

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

    // MARK: - UIPickerViewDelegate

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

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
        if(row == 0) {
            self.view.backgroundColor = UIColor.whiteColor();
        } else if(row == 1) {
            self.view.backgroundColor = UIColor.redColor();
        } else if(row == 2) {
            self.view.backgroundColor =  UIColor.greenColor();
        } else {
            self.view.backgroundColor = UIColor.blueColor();
        }
    }
}

Can someone explain why the UIPickerView is not working within the popover?

1

There are 1 answers

0
Leontien On

I've finally found my error. In my storyboard I was designing especially for iPad on the size class regular x regular. The popover shows the size class any x any. Therefore the view stayed empty. I designed the popover view controller in the size class any x any and now it is working.