This question already has answers here:
Unrecognized selector sent to instance NSTimer Swift
(3个答案)
5年前关闭。
我正在制作一个应用,您需要在其中指示标签显示的时间,并且该时间应在页面底部进行递减计数。
当我在Xcode模拟器上运行我的应用程序时,当我希望它仅显示指示的数字时,
还显示以下错误:
帮助将不胜感激。
以下是我的所有代码:
(3个答案)
5年前关闭。
我正在制作一个应用,您需要在其中指示标签显示的时间,并且该时间应在页面底部进行递减计数。
当我在Xcode模拟器上运行我的应用程序时,当我希望它仅显示指示的数字时,
chronometre
标签显示“可选(*指示的数字)”。一秒钟显示后,我进入我的App Delegate并显示Thread 1: signal SIGABRT
。还显示以下错误:
2015-02-22 21:05:50.860 MemoPres.New[71008:12980314] -[MemoPres_New.ViewController subtractTime:]: unrecognized selector sent to instance 0x7fbab24299c02015-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)"
}
}
}
最佳答案
您需要在IBAction外部声明方法减去时间()。这同样适用于您的计时器和第二个变量。
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))
}
}
关于ios - 错误-线程1:信号SIGABRT(无法识别的选择器发送到实例),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28666040/
10-14 19:52