1

I'm making an app where you need to indicate the amount of time that a label appears for and that amount of time is counted down at the bottom of the page.

When I run my app on the Xcode simulator, the chronometre label displays "Optional (*indicated number)" when I want it to only display the indicated number. Once it's displayed for a second, I get taken to my App Delegate and it says Thread 1: signal SIGABRT.

The following error is also shown: 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'.

Help would be greatly appreciated.

The following is all of my code:

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

    }


        }
}
Jacob
  • 23
  • 5

1 Answers1

2

You need to declare your method subtractTime() outside your IBAction. The same applies to your timer and second vars.

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 = Timer()
    var startTime: TimeInterval = 0
    var timeLeft: TimeInterval = 45
    var isTimerON = false
    var isPaused = false

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

    override func viewDidLoad() {
        super.viewDidLoad()
        timer = .scheduledTimerWithTimeInterval(1/20, target: self, selector: "updateTimer", userInfo: nil, repeats: true)
        RunLoop.main.addTimer(timer, forMode: .common)
    }

    @IBAction func pauseResumeAction(_ sender: Any) {
        isPaused = !isPaused
        if isPaused {
            timeLeft = startTime - Date().timeIntervalSinceReferenceDate
        }
        startTime = Date().dateByAddingTimeInterval(timeLeft).timeIntervalSinceReferenceDate
        btnPauseResume.setTitle( !isPaused ? "Pause" : "Resume", forState: .normal)
    }

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

        startTime = isTimerON ? Date().dateByAddingTimeInterval(timeLeft).timeIntervalSinceReferenceDate : timeLeft
        btnStartCancel.setTitle(!isTimerON ? "Start" : "Cancel",  forState: .normal)
        progressView.progress = 0
        btnPauseResume.setTitle("Pause",  forState: .normal)
        strTimer.text = "00:45"
        btnPauseResume.enabled = isTimerON
    }
}

extension TimeInterval {
    var time: String {
        String(format: "%02d:%02d", Int((self/60)%60), Int(self%60))
    }
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • I moved the function and the timer variable directly above the `presenterButton` function. But now it says that `func presenterButton` cannot be an IBAction because only instance methods can be declared 'IBAction'. Also, wherever `seconds` is mentioned in the `subtractTime` function, it says `Use of unresolved identifier 'seconds'`. Do you know how to fix those? – Jacob Feb 23 '15 at 02:42
  • I wouldn't create a timer to calculate time. Just use NSTimeInterval – Leo Dabus Feb 23 '15 at 02:49
  • Do you want me to show you a code showing how to create a timer I can but I won't try to fix something that started already the wrong way. – Leo Dabus Feb 23 '15 at 02:50
  • Yes, that would be great! @Leonardo Savio Dabus – Jacob Feb 23 '15 at 02:56
  • https://www.dropbox.com/s/de06vd6sxx6zz1x/progressBarIndicator.zip?dl=0 – Leo Dabus Feb 23 '15 at 03:15
  • You can finish it as a assignment. It will keep going even after 45 seconds – Leo Dabus Feb 23 '15 at 03:19
  • I do not need a Progress Bar right? @LeonardoSavioDabus – Jacob Feb 26 '15 at 12:08
  • Sure it is up to you display a visual representation of the progress or not – Leo Dabus Feb 26 '15 at 12:10
  • And I only have one button in my app that starts rolling through the points. Do I need to add more? @Leonardo – Jacob Feb 26 '15 at 12:11
  • You just have to initialize startime and update the text field with the result of the elapsed time calculation – Leo Dabus Feb 26 '15 at 12:13
  • Don't forget the ,time extension to convert your time interval to the formatted time string – Leo Dabus Feb 26 '15 at 12:14
  • So that can be placed in the same button? @Leonardo – Jacob Feb 26 '15 at 12:14
  • I will update the answer adapted to your question – Leo Dabus Feb 26 '15 at 12:16
  • Ok, please comment when you're done. I really appreciate your help. @Leonardo – Jacob Feb 26 '15 at 12:28