I am a beginner so I may not see obvious things at this point. So please, help. I need my startButton enabled again when pauseButton is pressed and vice versa. I cannot create a var that can be accessed by both UIButton functions though. What should I do? Here's part of my code:
#IBAction func Start(_ sender: AnyObject) {
let disableMyStartButton = sender as? UIButton
disableMyStartButton?.isEnabled = false
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ViewController.update), userInfo: nil, repeats: true)
}
#IBAction func pauseTimer(_ sender: AnyObject) {
timer.invalidate()
WarningLabel.text = "Paused"
}
You need to create Outlet of your buttons and use them in your methods.
#IBOutlet var btnStart: UIButton!
#IBOutlet var btnPause: UIButton!
#IBAction func start(_ sender: UIButton) {
sender.isEnabled = false
btnPause.isEnabled = true
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ViewController.update), userInfo: nil, repeats: true)
}
#IBAction func pauseTimer(_ sender: UIButton) {
sender.isEnabled = false
btnStart.isEnabled = true
timer.invalidate()
WarningLabel.text = "Paused"
}
You can also done this thing using single action for buttons like this.
#IBAction func onStartOrPause(_ sender: UIButton) {
if (sender == btnStart) {
sender.isEnabled = false
btnPause.isEnabled = true
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ViewController.update), userInfo: nil, repeats: true)
}
else {
sender.isEnabled = false
btnStart.isEnabled = true
timer.invalidate()
WarningLabel.text = "Paused"
}
}
Note: You can also make same thing using single button by changing title or image of it in the action.
Related
I am a complete beginner and am starting to learn the Swift programming language. I'm following a tutorial on Udemy and am having some problems with setting up a timer.
class ViewController: UIViewController {
#IBOutlet weak var label: UILabel!
var timer1 = Timer()
var counter = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
label.text = String(counter)
}
#IBAction func start(_ sender: Any) {
timer1 = Timer(timeInterval: 1, target: self, selector: #selector(tim), userInfo: nil, repeats: true)
}
#IBAction func pause(_ sender: Any) {
timer1.invalidate()
}
#IBAction func restart(_ sender: Any) {
timer1.invalidate()
counter = 0
label.text = String(counter)
}
#objc func tim() {
counter += 1
label.text = String(counter)
}
}
This is my code but the timer is not working. Please tell me where im going wrong.
You need to add timer to RunLoop
RunLoop.current.add(timer1, forMode: .commonModes)
Or use scheduledTimer
timer1 = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(tim), userInfo: nil, repeats: true)
Also in your restart function you need to do it again, because now you are just invalidating it and not starting again.
I am making timer. I cannot figure out how to make start button tapped only once as it is starting to count. And at stop button timer.invalidate() does not work
#IBAction func start(_ sender: Any) {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(W1ViewController.counter), userInfo: nil, repeats: true)
}
#IBOutlet weak var stopOutlet: UIButton!
#IBAction func stop(_ sender: Any) {
seconds = 30
label.text = "\(seconds)"
timer.invalidate()
audioPlayer.stop()
}
Just disable the button
#IBAction func start(_ sender: Any) {
stopOutlet.isEnabled = false
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(W1ViewController.counter), userInfo: nil, repeats: true)
}
Then re-enable when needed:
stopOutlet.isEnabled = true
UIButton extends UIControl. UIControl provides this and alot more functionality to all controls.
I'm trying to make a simple timer in iOS using Swift.
The program is working fine but whenever my START UIButton is pressed the function of timer starts and runs multiple time as much as the button is pressed.
I want to disable the START UIButton as soon as the timer function starts so that it does not run multiple times.
Please help me for the same.
This is my code of ViewController
import UIKit
class ViewController: UIViewController {
var time = 0.0
var timer = Timer()
#IBOutlet weak var lbl: UILabel!
#IBAction func start(_ sender: UIButton)
{
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ViewController.action), userInfo: nil, repeats: true)
}
#IBAction func pause(_ sender: UIButton)
{
timer.invalidate()
}
#IBAction func reset(_ sender: UIButton)
{
timer.invalidate()
time = 0.0
lbl.text = ("0")
}
func action()
{
time += 0.1
lbl.text = String(time)
}
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.
}
}
To disable your button you can just set it's isUserInteractionEnabled property in start function:
#IBOutlet weak var startButton: UIButton!
#IBAction func start(_ sender: UIButton) {
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ViewController.action), userInfo: nil, repeats: true)
startButton.isEnabled = false
}
Then set it back to true in pause and reset methods:
#IBAction func pause(_ sender: UIButton) {
timer.invalidate()
startButton.isEnabled = true
}
#IBAction func reset(_ sender: UIButton) {
timer.invalidate()
startButton.isEnabled = true
//the rest of your code
}
The most reliable way to ensure that the timer is only started and stopped once is writing two methods which check the current state (timer is not nil if it's currently running):
var timer : Timer?
func startTimer()
{
guard timer == nil else { return }
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ViewController.action), userInfo: nil, repeats: true)
// disable start button
}
func stopTimer()
{
guard timer != nil else { return }
timer!.invalidate()
timer = nil
// enable start button
}
In the methods you can enable/disable the button(s) accordingly.
I want to know how it is possible to grab the time from a UIDatePicker in Count Down Timer mode, and then display that while counting down in a Hour:Minute:Second format. My biggest issue is actually grabbing the time from the Date Picker. As I'm not really to sure how to start that.
override func viewDidLoad() {
super.viewDidLoad()
stopWatchPicker.countDownDuration = 60.0;
if timerCount == 0 {
timerRunning = false
}
}
var timerCount = 60 //set to 60 for testing purposes
var timer = Timer()
var timerRunning = false
#IBOutlet weak var stopWatchPicker: UIDatePicker!
#IBOutlet weak var countDownLabel: UILabel!
#IBAction func startButton(_ sender: AnyObject) {
if timerRunning == false {
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateCounter), userInfo: nil, repeats: true)
timerRunning = true
}
}
#IBAction func stopButton(_ sender: AnyObject) {
timer.invalidate()
timerRunning = false
}
func updateCounter() {
timerCount -= 1
countDownLabel.text = "\(timerCount)"
if timerCount == 0 {
timer.invalidate()
timerRunning = false
}
}
You need to grab the countDownDuration from the stopWatchPicker and assign it to the timerCount variable.
#IBAction func startButton(_ sender: AnyObject) {
if timerRunning == false {
timerCount = stopWatchPicker.countDownDuration
timerRunning = true
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateCounter), userInfo: nil, repeats: true)
}
}
Cheers!
I'm trying to create a simple countdown/Up timer app for myself. So far I've figured out how to create the timers which can count up and down with a start, stop and reset buttons but I can not create a multiple timer, I'm guessing its to do with the way I'm calling the function. I'm only guessing as its my first go at it.
Variables and Labels
var timerCountUp = 0
var timerCountDown = 45
var timerRunning = false
var timer = NSTimer()
#IBOutlet weak var timerUpLabel: UILabel!
func CountingUp(){
timerCountUp += 1
timerUpLabel.text = "\(timerCountUp)"
}
#IBOutlet weak var timerDownLabel: UILabel!
func CountingDown(){
timerCountDown -= 1
timerDownLabel.text = "\(timerCountDown)"
}
Stop and Rest Buttons
#IBAction func stopButton(sender: UIButton) {
if timerRunning == true{
timer.invalidate()
timerRunning = false
}
}
#IBAction func restartButton(sender: UIButton) {
timerCountUp = 0
timerUpLabel.text = "0"
}
For my start button I have the following
#IBAction func startButton(sender: UIButton) {
if timerRunning == false{
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("CountingUp"), userInfo: nil, repeats: true)
timerRunning = true
}
If my selector is either "CountingUp" or "CountingDown" I get one of them to work, so I did a bit of searching and came up with the following which does not work
#IBAction func startButton(sender: UIButton) {
if timerRunning == false{
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("timerStarted"), userInfo: nil, repeats: true)
timerRunning = true
}
func timerStarted() {
CountingUp()
CountingDown()
}
}
Would it be possible to show me where I have gone wrong please?