Search code examples
iosswiftuipickerview

It is always returning same index "picker.selectedRow(inComponent: 0)" in iOS swift


 func popUpList(title: String, PickerList: [String],index: Int, complistionHandler: @escaping (_ selectedValue: Int) -> ())  {
    let alertController = UIAlertController(title: nil, message: "\n\n\n\n\n\n\n", preferredStyle: .alert)
    alertController.isModalInPopover = true;
    let picker = UIPickerView(frame: CGRect(x: 0.0, y: 0.0, width: 250, height: 250))
    picker.delegate = self
    picker.dataSource = self
    picker.selectRow(index, inComponent: 0, animated: true)
    picker.showsSelectionIndicator = true
    list = PickerList

    picker.reloadAllComponents()
    alertController.view.addSubview(picker)
    let ok = UIAlertAction(title: "Ok", style: .default) { (alert) in
           print(picker.selectedRow(inComponent: 0))
        complistionHandler(picker.selectedRow(inComponent: 0))
    }
    alertController.addAction(ok)
    let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
    alertController.addAction(cancel)
    self.present(alertController, animated: true, completion: nil);
}

This Code is always returning value = 3. But I want to get value of index which is Selected. Please help me here


Solution

  • I tried your code. It turns out your picker is too big, its background is transparent and its in front of your buttons so its intercepting all of the touches. If you reduce the Y size it no longer conflicts. Similarly if you added more new lines it works. Also if you send the picker behind the buttons it would also work. If you use the view debugger you can see the extent of the picker and you can tell its over lapping and that its in front of the buttons. This code works:

        import UIKit
    
        typealias PopUpListCompletionHandler = (Int) -> (Void)
    
        class ViewController: UIViewController {
            fileprivate var list = [String]()
            override func viewDidAppear(_ animated: Bool) {
                super.viewDidAppear(animated)
    
                popUpList(title: "Pick a number from 1 to 10", PickerList: stride(from: 1, to: 11, by: 1).map {"\($0)"}, index: 5) { index in
                    print ("You picked index \(index) which has a value of \(self.list[index])")
                }
    
            }
    
    
            func popUpList(title: String, PickerList: [String],index: Int, completionHandler: @escaping PopUpListCompletionHandler)  {
                let alertController = UIAlertController(title: nil, message: "\n\n\n\n\n\n\n", preferredStyle: .alert)
                alertController.isModalInPopover = true;
                let picker = UIPickerView(frame: CGRect(x: 0.0, y: 0.0, width: 250, height: 200))
                picker.delegate = self
                picker.dataSource = self
                picker.selectRow(index, inComponent: 0, animated: true)
                picker.showsSelectionIndicator = true
                list = PickerList
    
                picker.reloadAllComponents()
                alertController.view.insertSubview(picker, at: 1)
                let ok = UIAlertAction(title: "Ok", style: .default) { (alert) in
                    completionHandler(picker.selectedRow(inComponent: 0))
                    self.dismiss(animated: true, completion: nil)
                }
                alertController.addAction(ok)
                let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
                alertController.addAction(cancel)
                self.present(alertController, animated: true, completion: nil);
            }
    
        }
    
        extension ViewController: UIPickerViewDelegate {
    
        }
    
        extension ViewController: UIPickerViewDataSource {
            func numberOfComponents(in pickerView: UIPickerView) -> Int {
                return 1
            }
    
            func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
                return list.count
            }
    
            func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
                return list[row]
            }
        }