我将 UIStepper 添加到水平 UIStackView 中,而水平 UIStackView 本身位于垂直 UIStackView 上。问题是,无论我做什么,UIStepper 都不会注册任何事件。
我的代码如下
init(variableIndex: Int, variableName: String, withParentView parent: UIView, currentValue: String, readOnly: Bool) {
// Instantiate the properties
valueTextField = UITextField()
valueStepper = UIStepper()
numberOfDecimals = currentValue.components(separatedBy: ".").count > 1 ? currentValue.components(separatedBy: ".")[1].count : 1
numericValue = Double(currentValue)!
super.init(variableIndex: variableIndex, variableName: variableName, withParentView: parent)
// Horizontal Stack View
let horizontalStackView = UIStackView()
horizontalStackView.axis = .horizontal
horizontalStackView.alignment = .fill
horizontalStackView.spacing = 15
horizontalStackView.distribution = .fill
horizontalStackView.translatesAutoresizingMaskIntoConstraints = false
horizontalStackView.isUserInteractionEnabled = true
// Text Field
valueTextField.text = stringValue
valueTextField.textAlignment = .right
valueTextField.layer.borderWidth = 1.0
valueTextField.layer.cornerRadius = 6.0
valueTextField.layer.borderColor = UIColor.darkGray.cgColor
#warning ("TODO: Enable interaction, change keyboard to numeric and add done button to keyboard")
valueTextField.isUserInteractionEnabled = false
valueTextField.rightView = UIView(frame: CGRect(x: 0, y: 0, width: ConfigFile.rightPadding, height: Int(valueTextField.frame.size.height)))
valueTextField.rightViewMode = .always
// Stepper
valueStepper.value = numericValue
valueStepper.stepValue = 0.1
valueStepper.addTarget(self, action: #selector(self.stepperValueChanged(sender:)), for: .touchUpInside)
valueStepper.addTarget(self, action: #selector(self.test(sender:)), for: .allEditingEvents)
valueStepper.isEnabled = !readOnly
// Create the constraints
constraints.append(NSLayoutConstraint(item: horizontalStackView, attribute: .width, relatedBy: .equal, toItem: stackView, attribute: .width, multiplier: 1.0, constant: 0.0))
horizontalStackView.addArrangedSubview(valueTextField)
horizontalStackView.addArrangedSubview(valueStepper)
stackView.addArrangedSubview(horizontalStackView)
}
@objc private func stepperValueChanged(sender: UIStepper) {
numericValue = valueStepper.value
let userInfo = [
"VariableIndex": String(self.variableIndex),
"NewValue": self.stringValue
]
NotificationCenter.default.post(name: .numericVariableChanged, object: nil, userInfo: userInfo)
}
@objc private func test(sender: UIStepper) {
print("Hi")
}
如您所见,我用以下行添加了事件
valueStepper.addTarget(self, action: #selector(self.stepperValueChanged(sender:)), for: .touchUpInside)
valueStepper.addTarget(self, action: #selector(self.test(sender:)), for: .allEditingEvents)
我还尝试在第二行使用 .allTouchEvents
参数,但没有成功。
UIStepper 上方的所有 View 都将其 isUserInteractionEnabled
属性设置为 true
在执行过程中,当我按下 UIStepper 时,我会收到来自 UIStepper 的反馈(它会暂时改变颜色)。
最佳答案
将 .valueChanged
事件与 UIStepper
结合使用,而不是 .touchUpInside
或 .allEditingEvents
。
关于ios - UIStepper不会注册任何事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55266144/