首页 > 解决方案 > 如何防止在拖动 UIPickerView 时触发按钮?

问题描述

我创建了一个自定义操作表,其中包含一个 UIPickerView 和两个按钮(见下图)。

当我向下拖动滚动时,我的手指(我在屏幕截图上表示它)在它“悬停”它时按下按钮。

最终我将手指放在取消按钮上,因此将触发取消事件(当然,这同样适用于 Ok 按钮)。

我应该怎么做才能不发生?

在此处输入图像描述

我在我的应用程序中创建各种操作表的代码如下:

/**
 Creates and present action sheet
 */
public func createAndPresentActionSheet(_ parent: UIViewController, title: String, message: String, tag: PickerDataSource, _ completion: @escaping ((_ action: UIAlertAction)->())) {

    let vc = UIViewController()
    vc.preferredContentSize = CGSize(width: 250,height: 200)

    let pickerView = UIPickerView()
    pickerView.isExclusiveTouch = true
    pickerView.tag = tag.rawValue
    pickerView.delegate   = parent as? UIPickerViewDelegate
    pickerView.dataSource = parent as? UIPickerViewDataSource
    vc.view.addSubview(pickerView)
    pickerView.stitchWithConstraints(to: vc.view)

    var row = 0
    switch tag {
    case .gas:                      row = -1 + GasStation.carGas.rawValue

    case .averageSpeed:             row = -1 + Int(GasStation.carAverageSpeed*1e-3)
    case .consumptionPer100km:      row = -1 + Int(GasStation.carGasConsumptionPer100km)
    case .tankVolume:               row = -1 + Int(GasStation.carGasTankVolume)
    case .usualVolumeRefill:        row = -1 + Int(GasStation.carGasUsualRefill)

    case .databaseMinimumFreshness: row = 0
    }

    print("\(#function): row = \(row)")

    if row < 0 { row = 0 }

    print("\(#function): row = \(row) once fixed from being negative")

    pickerView.selectRow(row, inComponent: 0, animated: true)



    let alert = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
    alert.setValue(vc, forKey: "contentViewController")
    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in

        if let tvc = parent as? UITableViewController {
            // Reload from data source
            tvc.tableView.reloadData()

            // Force display
            //tvc.tableView.setNeedsDisplay()
        }
        // Call completion
        return completion(action)
    }))
    alert.addAction(UIAlertAction(title: "Annuler", style: .cancel, handler: nil))
    parent.present(alert, animated: true)
}

标签: swiftuibuttonuipickerviewuiactionsheetios12

解决方案


我可以看看你设置 UIAlertController 的代码吗?我试图复制你的 UIAlertController 并没有得到你描述的问题。


推荐阅读