【问题标题】:"unrecognized selector sent to instance" error in swiftswift中的“无法识别的选择器发送到实例”错误
【发布时间】:2020-05-16 11:01:14
【问题描述】:

我正在创建一个秒表,因此我创建了一个按钮并添加了一个动作来启动秒表,但是一旦调用它,我就会收到此错误:

2020-05-13 19:46:02.543555+1000 Timer Pro[27206:2613795] -[Timer_Pro.Stopwatch2ViewController updateStopwatch]: unrecognized selector sent to instance 0x7fb4d7425390

(lldb) 

我正在做的是在我的秒表中包含分钟、秒和分数,它从 0 开始。每 0.01 秒,分数就会上升,在 99 时,它会在秒表上加一并重置分数,当秒数达到 59 它会重置并在分钟上加一。我有一个圈表,当按下圈按钮时,它会将时间放在圈表上。在我的用户界面上,我有一个开始按钮,按下它会变成一个停止按钮,在停止/开始按钮旁边我有一个圈按钮,按下它会变成一个重置按钮。我在一个名为 stop.png、start.png、reset.png 和 lap.png 的文件中有 4 个按钮。这是我的代码:

import UIKit

class Stopwatch2ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


    var timer = Timer()
    var minutes: Int = 0
    var seconds: Int = 0
    var fractions: Int = 0

    var stopWatchString: String = ""

    var startStopWatch: Bool = true
    var addLap: Bool = false

    @IBOutlet weak var stopwatchLabel: UILabel!

    @IBOutlet weak var lapsTableView: UITableView!

    @IBOutlet weak var startstopButton: UIButton!
    @IBOutlet weak var lapresetButton: UIButton! 

    @IBAction func startStop(_ sender: Any) {

        if startStopWatch == true {

            timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: Selector(("updateStopwatch")), userInfo: nil, repeats: true)
            startStopWatch = false

            startstopButton.setImage(UIImage(named: "stop.pnng"), for: UIControl.State.normal)
            lapresetButton.setImage(UIImage(named: "lap.png"), for: UIControl.State.normal)

            addLap = true

        }else {

            timer.invalidate()
            startStopWatch = true

            startstopButton.setImage(UIImage(named: "start.png"), for: .normal)
            lapresetButton.setImage(UIImage(named: "reset.png"), for: .normal)

            addLap = false

        }


    }

    @IBAction func lapReset(_ sender: Any) {

    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        stopwatchLabel.text = "00:00.00"
    }

    func updateStopWatch() {

        fractions += 1
        if fractions == 100 {

            seconds += 1
            fractions = 0
        }

        if seconds == 60 {

            minutes += 1
            seconds = 0
        }

        let fractionsString = fractions > 9 ? "\(fractions)" : "0\(fractions)"
        let secondsString = seconds > 9 ? "\(seconds)" : "0\(seconds)"
        let minutesString = minutes > 9 ? "\(minutes)" : "0\(minutes)"

        stopWatchString = "\(minutesString):\(secondsString).\(fractionsString)"
        stopwatchLabel.text = stopWatchString

    }



    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = UITableViewCell(style: UITableViewCell.CellStyle.value1, reuseIdentifier: "Cell")

        cell.backgroundColor = self.view.backgroundColor

        cell.textLabel?.text = "Lap"
        cell.detailTextLabel?.text = "00:00:00"


        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 3
    }

}

如果有人能帮助我,我将不胜感激,因为我是初学者,这个错误让我几个月来一直熬夜。

【问题讨论】:

  • 您的选择器语法错误;你需要在函数名后面加一个冒号,所以 Selector("updateStopwatch:") 不是 Selector("updateStopwatch")

标签: swift selector unrecognized-selector


【解决方案1】:

您正在创建Selector("updateStopwatch"),但您的方法称为func updateStopWatch()

请改用#selector(self.updateStopWatch),这只允许您在实际存在的方法上创建选择器。

还要确保选择器引用的方法必须对 Objective-C 运行时可见,因此您需要将它们标记为 @objc

【讨论】:

  • 对不起,我试过了,但没有成功,它说 '#selector' 的参数没有引用 @objc 方法、属性或初始化程序。
  • 是的。您只能让选择器指向 @objc 方法。将修改我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多