swift - 如何防止在拖动 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)
}
解决方案
我可以看看你设置 UIAlertController 的代码吗?我试图复制你的 UIAlertController 并没有得到你描述的问题。
推荐阅读
- java - Jackson XML基于命名空间反序列化列表
- node.js - DatabaseError [SequelizeDatabaseError]:值超出整数类型的范围
- php - 在 PHP 中从数组中过滤某人字符串的问题
- python - 解释为什么 Python 在某些递归代码上崩溃?
- java - 多个源文件夹的 Intellij 问题
- security - 为什么让我们加密 SSL 证书从 2021 年 9 月 30 日起在某些(客户端)PC 中不起作用?
- python - 根据类别python填充另一列的值
- javascript - 如何使用 Handlebars.JS 正确创建 If/Else 语句?
- excel - Excel 中的甘特图 - 将小时数添加到开始时间而不是天数 - 每周计划
- dictionary - 从 SOQL 查询填充 Apex 地图