ios - 它总是在 iOS swift 中返回相同的索引 "picker.selectedRow(inComponent: 0)"

标签 ios swift uipickerview

 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);
}

此代码始终返回值 = 3。但我想获取已选择的索引值。请在这里帮助我

最佳答案

我试过你的代码。原来你的选择器太大了,它的背景是透明的,而且它在你的按钮前面,所以它拦截了所有的触摸。如果减小 Y 大小,则不再冲突。同样,如果您添加更多新行,它会起作用。此外,如果您将选择器发送到按钮后面,它也可以工作。如果你使用 View 调试器,你可以看到选择器的范围,你可以告诉它重叠,它在按钮前面。此代码有效:

    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]
        }
    }

关于ios - 它总是在 iOS swift 中返回相同的索引 "picker.selectedRow(inComponent: 0)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40044535/

相关文章:

ios - 数组索引超出范围跳过功能

ios - 无法从应用程序组 ios 中的共享容器中读取

ios - 如何在 Xcode 中获得一个标签来显示星期几 (Swift/Swift 2)

swift - WatchOS 从通知打开新界面而不启动应用程序

ios - Swift:使用scenekit 中的projectPoint 随时间获取并保存更新的SCNNode

ios - 自定义 iOS UINavigationBar/UITabBar 和移除 Tint Colors

json - swift & JSON : array of objects

swift - PickerView - ViewController 不符合协议(protocol) UIPickerViewDataSource

ios - 工作几天后,UITextField.inputView 突然不弹出 UIPickerView

iphone - UIDatepicker 日期只显示星期一的日期