0

I am trying to incorporate a UIStepper into a UITextField, while incrementing the value of the stepper, the UITextField updates with it to that value. I keep getting the error:

"Terminating app due to uncaught exception ''NSInvalidArgumentException', reason: '-[Calculator.SplitViewController stepperValueChanged:]: unrecognized selector sent to instance 0x7fd730c0af30"`

Whenever I run my app and tap the "+" button on the stepper.

Any solutions? I checked my selector function and I think it looks right.

override func viewDidLoad() {
    super.viewDidLoad()

    partyOfStepper.autorepeat = true
    partyOfStepper.minimumValue = 1
    partyOfStepper.maximumValue = 99
    partyOfTextField.text = "\(Int(partyOfStepper.value))"
    partyOfStepper.addTarget(self, action: #selector(stepperValueChanged(stepper:)), for: .valueChanged)

}


@IBAction func calculateButtonTapped(_ sender: Any) {
}


func stepperValueChanged(stepper: UIStepper) {

    let stepperMapping: [UIStepper: UITextField] = [partyOfStepper: partyOfTextField]
    stepperMapping[stepper]!.text = "\(Int(stepper.value))"

}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
Gar
  • 901
  • 1
  • 7
  • 9
  • Update your question with the complete and exact error message. You left out the import parts of the message. – rmaddy Jan 10 '17 at 03:56
  • You should bind the button to `calculateButtonTapped`, and update the stepper value by `stepperValueChanged `. – Zigii Wong Jan 10 '17 at 03:56
  • Check in the interface builder(storyboard) you have set action for that `UIStepper` simply remove that action and it will works for you. – Nirav D Jan 10 '17 at 05:04

1 Answers1

1

Your code works for me, as Nariv said make sure you haven't previously created an action in the designer for the UIStepper (check in the Connections inspector) :

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var partyOfStepper: UIStepper!
    @IBOutlet weak var partyOfTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        partyOfStepper.autorepeat = true
        partyOfStepper.minimumValue = 1
        partyOfStepper.maximumValue = 99
        partyOfTextField.text = "\(Int(partyOfStepper.value))"
        partyOfStepper.addTarget(self, action: #selector(stepperValueChanged(stepper:)), for: .valueChanged)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func stepperValueChanged(stepper: UIStepper) {

        let stepperMapping: [UIStepper: UITextField] = [partyOfStepper: partyOfTextField]
        stepperMapping[stepper]!.text = "\(Int(stepper.value))"

    }

}
CoderMike
  • 301
  • 1
  • 7