ios - UIStepper不会注册任何事件

标签 ios swift uikit uistepper

我将 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/

相关文章:

ios - -bash : cd: supbot-tut: No such file or directory

ios - 找不到协议(protocol)声明 ios7

ios - 使用结构在 Swift 5 中使用 JSONDecoder 解析 JSON

ios - 如何使用 Swift 删除一个部分中的一行?

ios - 不能在 inout 函数中使用 SKShapeNode

ios - 从旧 Swift 到 Swift 3

swift - 看不到 UIView 的 UIView subview

iphone - iOS 7 中的 UITabBar 色调

ios - 快速弹出 anchor

ios - APP_Browsers 文件夹在项目中的意义以及名为 whateveryouwant.browser 的文件的意义是什么