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)"
}
}
}
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 = 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 ))
}
}
来源:https://stackoverflow.com/questions/28666040/error-thread-1-signal-sigabrt-unrecognized-selector-sent-to-instance