将选择器添加到UIStepper的addTarget函数时出错 - Swift 3

时间:2017-01-10 03:51:12

标签: swift swift3 xcode8 uistepper addtarget

我正在尝试将UIStepper合并到UITextField中,同时递增步进器的值,UITextField将其更新为该值。我一直收到错误:

  

“由于未捕获的异常终止应用程序''NSInvalidArgumentException',原因:' - [Calculator.SplitViewController stepperValueChanged:]:无法识别的选择器发送到实例0x7fd730c0af30”`

每当我运行我的应用程序并点击步进器上的“+”按钮时。

任何解决方案?我检查了我的选择器功能,我认为它看起来是正确的。

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

}

1 个答案:

答案 0 :(得分:1)

你的代码对我有用,因为Nariv说确保你之前没有在设计器中为UIStepper创建一个动作(检查Connections检查器):

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

    }

}