错误 - 线程1:信号SIGABRT(无法识别的选择器发送到实例)

时间:2015-02-23 02:19:39

标签: ios xcode swift nstimer

我正在创建一个应用程序,您需要指明标签显示的时间量,并在页面底部对该时间进行倒计时。

当我在Xcode模拟器上运行我的应用程序时,chronometre标签显示“可选(*指示的数字)”,当我希望它只显示指定的数字时。一旦显示一秒钟,我就会被带到我的应用代表处,它会显示Thread 1: signal SIGABRT

还会显示以下错误:2015-02-22 21:05:50.860 MemoPres.New[71008:12980314] -[MemoPres_New.ViewController subtractTime:]: unrecognized selector sent to instance 0x7fbab24299c0 2015-02-22 21:05:50.862 MemoPres.New[71008:12980314] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MemoPres_New.ViewController subtractTime:]: unrecognized selector sent to instance 0x7fbab24299c0'

非常感谢帮助。

以下是我的所有代码:

class ViewController: UIViewController {


@IBOutlet weak var chronometre: UILabel!
@IBOutlet weak var prochainpointLabel: UILabel!
@IBOutlet weak var pointactuelLabel: UILabel!
@IBOutlet weak var point1Textfield: UITextField!
@IBOutlet weak var point1tempsTextfield: UITextField!
@IBOutlet weak var point2Textfield: UITextField!
@IBOutlet weak var point2tempsTextfield: UITextField!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func presenterButton(sender: AnyObject) {

    var timer = NSTimer()
    var seconds = point1tempsTextfield.text.toInt()

    pointactuelLabel.text = point1Textfield.text
    prochainpointLabel.text = point2Textfield.text
    chronometre.text = "\(seconds)"



        timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: ("subtractTime:"), userInfo: nil, repeats: true)



    func subtractTime(dt:NSTimer){
        seconds!--
        chronometre.text = "\(seconds)"

    }


        }
}

1 个答案:

答案 0 :(得分:2)

您需要在IBAction之外声明方法subtractTime()。这同样适用于您的计时器和第二个变量。

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var progressView: UIProgressView!
    @IBOutlet weak var strTimer: UILabel!
    @IBOutlet weak var btnStartCancel: UIButton!
    @IBOutlet weak var btnPauseResume: UIButton!

//

    var timer = NSTimer()
    var startTime:NSTimeInterval = 0
    var timeLeft:NSTimeInterval = 45
    var isTimerON = false
    var isPaused = false

//

    func updateTimer(){
        if isTimerON && !isPaused {
            strTimer.text = (startTime - NSDate().timeIntervalSinceReferenceDate  ).time
            progressView.progress = (1 - Float( startTime - NSDate().timeIntervalSinceReferenceDate  ) / 45 )
        }
    }

//

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        timer = NSTimer.scheduledTimerWithTimeInterval(1/20, target: self, selector: "updateTimer", userInfo: nil, repeats: true)
        NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)

    }

//

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

//

    @IBAction func pauseResumeAction(sender: AnyObject) {
        isPaused = !isPaused
        if isPaused {
            timeLeft = startTime - NSDate().timeIntervalSinceReferenceDate
        }
        startTime = NSDate().dateByAddingTimeInterval(timeLeft).timeIntervalSinceReferenceDate
        btnPauseResume.setTitle({return !self.isPaused ? "Pause":"Resume" }(), forState: UIControlState.Normal)

    }

//

    @IBAction func startCancelAction(sender: AnyObject) {
        isTimerON = !isTimerON
        isPaused = false
        timeLeft = 45

        startTime = isTimerON ? NSDate().dateByAddingTimeInterval(timeLeft).timeIntervalSinceReferenceDate : timeLeft
        btnStartCancel.setTitle({return !self.isTimerON ? "Start" : "Cancel"}(),  forState: UIControlState.Normal)
        progressView.progress = 0
        btnPauseResume.setTitle("Pause",  forState: UIControlState.Normal)
        strTimer.text = "00:45"
        btnPauseResume.enabled = isTimerON ? true : false
    }
}

//

extension NSTimeInterval {
    var time:String {
        return String(format:"%02d:%02d", Int((self/60.0)%60), Int((self) % 60 ))
    }
}