UIStepper events won't trigger

I'm adding a UIStepper to a horizontal UIStackView which itself is on a vertical UIStackView. The problem is that no matter what I do, the UIStepper won't register any event.

My code is the following


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


As you can see, I add the events in the lines 32 and 33.


I also tried using the

.allTouchEvents
and the .valueChanged arguments but that didn't work either.

All the views above the UIStepper have their

isUserInteractionEnabled
property set to
true

During execution, I get feedback from the UIStepper when I press it (it changes color for a moment).

Accepted Reply

Problem was that I wasn't keeping a reference to the object after initializing it so it was being deinitialized immediately after creation.
I solved this by keeping a reference to it in the ViewController that holds it.

Replies

In IB, I tried including a stepper in a stack view inside a stackview. It works smoothly.


The connected event is value changed.


Line 32, test a different event


valueStepper.addTarget(self, action: #selector(self.test(sender:)), for: .valueChanged)


and add a log line 33


print("readOnly", readOnly)

Maybe readOnly is true, hence stepper is disabled

Problem was that I wasn't keeping a reference to the object after initializing it so it was being deinitialized immediately after creation.
I solved this by keeping a reference to it in the ViewController that holds it.

Where did you declare stepper ?


You create in init by

valueStepper = UIStepper()


so, valueStepper must have been declare at a class scope I assume.