Does PickerView appear as question marks instead of data?

I am trying to add pickerview to my iphone application, but instead of showing a string from my array, it shows question marks. Does anyone know why? I tried to figure this out in the last hour ... Here is my controller code that contains pickerview:

class NewIssueViewController: UIViewController, UIPickerViewDelegate { var componentArr = ["component1","component2","component3","component4"] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func CancelPressed(sender: AnyObject) { self.dismissViewControllerAnimated(true, completion: nil) } @IBAction func AddPressed(sender: AnyObject) { self.dismissViewControllerAnimated(true, completion: nil) } func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int { return 1 } func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int { return componentArr.count } func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! { return componentArr[row] } } 
+6
source share
4 answers

You specify your data source for selection, but the delegate did not specify it.

As a result, pickerView:titleForRow: never called.

+16
source

You must either PickerView Datasource and Delegate in the storyboard, or install them programmatically. See image below.

enter image description here

0
source

The new Swift 3 does not recommend the use of the methods described above.

I did the following to make them work. (Some underline added in parameter.

  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] } 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(); }*/ } 
0
source

Just add self.pickerOutletReference.delegate = self to viewDidLoad () menthod

0
source

Source: https://habr.com/ru/post/977734/


All Articles