Timer setting not working in iOS - ios

I have a problem with timer setting.
The code I wrote for it is:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: Selector(("updateTimer")), userInfo: nil, repeats: true)
}
func updateTimer(){
label.text = DateFormatter.localizedString(from: NSDate() as Date, dateStyle: DateFormatter.Style.short, timeStyle: DateFormatter.Style.short)
}
}
I run the app, and instead of timer I get an error of type Thread.
Something like:
"Thread 1: signal SIGBART"
What could be the problem?

Things will work better if you replace Selector(("updateTimer")) with #selector(updateTimer).
Then you'll be told you need to add #objc to your updateTimer function.
Unrelated but don't use NSDate.
Here's your fixed code:
class ViewController: UIViewController {
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
}
#objc func updateTimer() {
label.text = DateFormatter.localizedString(from: Date(), dateStyle: .short, timeStyle: .short)
}
}

Change your code to this:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
}
#objc func updateTimer(){
label.text = DateFormatter.localizedString(from: NSDate() as Date, dateStyle: DateFormatter.Style.short, timeStyle: DateFormatter.Style.short)
}
}

Related

unrecognized selector sent to instance 0x7fb8bf50b3c0' (Swift/Xcode)

I am trying to learn a new language (Swift/Xcode) and I don't seem too be able to run the following example:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var test: UIBarButtonItem!
func processTimer(){
print("a second has passed")
}
override func viewDidLoad() {
super.viewDidLoad()
var timer = Timer() // Create timer object
timer = Timer.scheduledTimer(timeInterval: 1,
target: self,
selector: Selector("processTimer"),
userInfo: nil,
repeats: true)
}
}
I am getting:
Thread 1 :signal SIGABRT ????
Please, don't forget to ask a question next time. I think you should change your code into:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var test: UIBarButtonItem!
#objc func processTimer(){
print("a second has passed")
}
override func viewDidLoad() {
super.viewDidLoad()
var timer = Timer() // Create timer object
timer = Timer.scheduledTimer(timeInterval: 1,
target: self,
selector: Selector(processTimer),
userInfo: nil,
repeats: true)
}
}
And then everything should work.

Time delay of texts in label in swift 3

I have a label. And I have 3 strings. I need to display text of 3 strings in the same label with delay of 10 seconds over a infinite loop. How can i solve this with simple animations in swift 3?
That's my solution. Just connect a UILabel to the IBOutlet
class ViewController: UIViewController {
#IBOutlet weak var textLabel: UILabel!
let messages = ["PROFESSIONAL AND BEST LEARNING CENTER","LEARNING TECHNOLOGY AND DESIGN IN A SMART WAY","EXPLORE YOUR SKILLS"]
let delayTime = 10.0
var counter = 0
override func viewDidLoad() {
super.viewDidLoad()
let timer = Timer.scheduledTimer(timeInterval: delayTime, target: self, selector: #selector(changeDisplayedText), userInfo: nil, repeats: true)
timer.fire()
}
func changeDisplayedText() {
textLabel.text = messages[counter % messages.count]
counter += 1
}
}
This will work for your, connect your outlet properly and declare those string in an array and load it with timer change.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var label: UILabel!
var array = ["Aaaaaaaaaaa", "Bbbbbbbbbbb", "Ccccccccccc"]
var scrollIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
let timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(self.myDelayedFunction), userInfo: nil, repeats: true)
timer.fire()
}
func myDelayedFunction()-> Void {
let count = self.array.count
if scrollIndex == count {
scrollIndex = 0
}
if scrollIndex < count {
if count > 1{
self.label.text = array[scrollIndex]
self.scrollIndex += 1
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Swift: Why my label doesn't update even I employ a timer to change its text attribute?

I just want to implement a simple clock, and here is my storyboard:
Code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var timeLabel: UILabel!
let dateFormatter = DateFormatter()
override func viewDidLoad() {
super.viewDidLoad()
dateFormatter.dateFormat = "hh:mm:ss"
_ = Timer.init(timeInterval: 0.1, target: self, selector: #selector(updateClock), userInfo: nil, repeats: true)
}
func updateClock() {
self.timeLabel.text = dateFormatter.string(from: Date())
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The label just keeps displaying 00:00:00. Could anyone please give me some suggestion about that?
You have to create a Timer (which you have done) and schedule it. That adds it to the run loop so it will actually fire.
The easiest way to do it is to use Timer.scheduledTimer(timeInterval:target:selector:userInfo:repeats:), which both creates and schedules the Timer:
_ = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(updateClock),
userInfo: nil, repeats: true)
Note that you should probably also store a weak reference to the Timer, so you can invalidate it when you want it to stop firing.

Simple swift Stopwatch don't work

This is my code.
//
// ViewController.swift
// Stopwatch
//
// Created by Orkun Atasoy on 12.09.15.
// Copyright (c) 2015 Orkun Atasoy. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var timerLabel: UILabel!
#IBOutlet weak var timerButton: UIButton!
var timer : NSTimer?
var ms = 0
#IBAction func buttonTapped(sender: AnyObject) {
timerButton.setTitle("Stopp", forState:UIControlState.Normal)
self.timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
}
func update() {
self.ms++
timerLabel.text = String(self.ms)enter code here
}
}
The Problem is when i run the build it comes the introscreen with big text "Stopwatch" and the it is like freezed there. But it should come a label with button downside which has a text "start". When i click the button it should start counting and the label should change to "stopp". When i click again the it should stop the timer.
I dont get what the Problem ist. I am a Swift newbie. I would be pleased if you could help me.
Thank you for your attention
EDIT >> the label text is at the beginning "00:00".
Looks like you are messing with IBOutlet connections and I have corrected some of your code and here is working code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var timerLabel: UILabel!
#IBOutlet weak var timerButton: UIButton!
var timer : NSTimer?
var ms = 0
override func viewDidLoad() {
timerLabel.text = "00:00"
}
#IBAction func buttonTapped(sender: AnyObject) {
if timerButton.currentTitle == "Stopp" {
timer?.invalidate()
timerButton.setTitle("Start", forState:UIControlState.Normal)
} else {
timerButton.setTitle("Stopp", forState:UIControlState.Normal)
self.timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
}
}
func update() {
self.ms++
timerLabel.text = String(self.ms)
}
}
And First of all remove all outlets from your storyboard view controller and connect it this way:
And if you want check THIS sample project.
The method you call when the timer fires is incorrect. From the documentation:
The selector should have the following signature: timerFireMethod: (including a colon to indicate that the method takes an argument). The timer passes itself as the argument, thus the method would adopt the following pattern:
- (void)timerFireMethod:(NSTimer *)timer
So your method should be:
func update(timer: NSTimer) {
self.ms++
timerLabel.text = String(self.ms)
}
And you should set up your timer as:
NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "update:", userInfo: nil, repeats: true)
There are a few more things I could mention - such as no need to use self when unambigously using variables, and using a dispatch timer instead of an NSTimer, but this should at least solve your immediate problem.

How to stop NSTimer.scheduledTimerWithTimeInterval

How do i stop my timer from running? Not like a pause, but a stop.
import UIKit
class LastManStandingViewController: UIViewController {
#IBOutlet weak var timeLabel: UILabel!
#IBOutlet weak var timeTextbox: UITextField!
#IBOutlet weak var startButton: UIButton!
#IBOutlet weak var stopButton: UIButton!
var myCounter = 0
var myTimer : NSTimer = NSTimer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
timeLabel.text = String(myCounter)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func startTimer(){
myTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTimer"), userInfo: nil, repeats: true)
println("func startTimer")
}
func stopTimer(){
myTimer.invalidate()
myCounter = 0
timeLabel.text = String(myCounter)
println("func stopTimer")
}
func updateTimer(){
timeLabel.text = String(myCounter++)
println("func updateTimer")
}
#IBAction func startButton(sender: AnyObject) {
startTimer()
}
#IBAction func stopButton(sender: AnyObject) {
stopTimer()
}
}
I can start the timer, but when i press the Stop button, it reset itself, and starts counting again. It doesn't stop.
Made it work. Something was buggy with my project! Fixed it by removing the button and re-adding them. Looks like i had a duplicate or something.
You don't have to use Selector:
#IBAction func startButton(sender: AnyObject) {
myTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updateTimer:", userInfo: nil, repeats: true)
}
Also, the timer passes itself to the selected method, so you can invalidate it inside the method if you need:
func updateTimer(timer: NSTimer) {
timeLabel.text = String(Counter++)
timer.invalidate()
}
Or if the timer is an instance variable:
myTimer.invalidate()
myTimer = nil
It's a good thing to nil the instance variable timer after having invalidated it, it avoids further confusion if you need to create another timer with the same variable. Also, method names and variables should begin with a lowercase letter.
Screenshot to show the timer invalidated and set to nil.
Update for Swift 2.2+
See https://stackoverflow.com/a/36160191/2227743 for the new #selector syntax replacing Selector().
you can use this when some condition is met and you want to stop timer:
Timer.invalidate()
Here is simple example :
func UpdateTimer(){
timeLabel.text = String(Counter++)
if timeLabel.text == String("5") {
Timer.invalidate()
}
}
this will stop timer.
You can modify it as per your need.
As pre Swift 2.2
let printTimer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: #selector(printDescription), userInfo: nil, repeats: true)
func printDescription() {
print("Print timer will print every after 2.0 seconds")
}
Print timer will print every after 2.0 seconds

Resources