How do I add a reset function?

How would I add a reset function to the IBOutlet, currently when the button is clicked then the timer will start and when the same button is clicked again it will stop, but when it is clicked a third time it will start again, but I want it to reset.

@IBAction func start(sender: AnyObject) { if !timer.valid{ let aSelector : Selector = "updateTime" timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: aSelector, userInfo: nil, repeats: true) startTime = NSDate.timeIntervalSinceReferenceDate() stoptimer.text = String(arc4random_uniform(4)+1) } else{ timer.invalidate() } }


Why not save a button state in a global variable, so you can do what you want?

Here is a quick example: (you can chance the title of the button here too)

var buttonState:Int = 0;

// func for buttonpress

@IBAction func startButtonClicked(sender: UIButton) {

switch(buttonState) {
case 0:
// button first pressed, run code

// set state to 1:
buttonState = 1;

case 1:
// button second tap, run code

// set state to 2:
buttonState = 2;

case 2:
// button third tap, run code

// reset state to 0:
buttonState = 0;


You can set one bool for it as shown in below code:

var stopTime = false

@IBAction func start(sender: AnyObject) {

if !timer.valid && !stopTime{

let aSelector : Selector = "updateTime"
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: aSelector, userInfo: nil, repeats: true)
var startTime = NSDate.timeIntervalSinceReferenceDate()
stoptimer.text = String(arc4random_uniform(4)+1)

}else if stopTime{

//reset you timer here
//reset your bool as per your need.

} else {

stopTime = true

When you click on start button when your timer is running it will enter into else and set your stopTime bool to true so that next time when you press start button it will not enter into that if condition because stopTime is true this time.

Or without boolean

In your class, declare your timer as optional

var timer: NSTimer?

In your @IBAction function:

if timer != nil {
timer = nil
} else {
timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
// Do everything you want with your timers

Category:ios Time:2019-01-10 Views:1
Tags: xcode ios swift

Related post

  • How do I add an OR function to a PHP code? 2011-01-14

    Firstly, dont laugh, I know it is basic. I have this code: if ($pageid == '9') { echo... and I want to add an OR to it, so I tried: if ($pageid == '9' or '5') { echo... but it didnt work. How do I add an OR function to this? --------------Solutions--

  • How can I add a reset button on a flash file? 2011-03-26

    How can I make a reset button so when the file ends it resets all variables and code to the default run-time code and frame? --------------Solutions------------- Just add a button to the stage on the last frame (or where you want to), let's say "rese

  • How can i add different language functionality in my website 2011-07-29

    I need help. I have a Html template which i bought from Template Monster. I want to add 3 languages with flag to the top right corner in my template. This html template with two form. One is booking and other is contact. Language is.. English, Swedis

  • How can i add upload image function inside a ajax function? 2012-10-28

    i have a dilema here im using if else inside an ajax function i have this code function submitForm() { offset = jQuery('#email').offset(); xTop = - $(window).height()/2 ; posLeft = offset.left - ($(window).width()-765)/2 ; posTop = offset.

  • How can i add a new Function or Property on textbox class's text property 2010-04-23

    I want to add one more function/property to a text property of a textbox like this. txtControl.Text.IsEmpty(); or txtControl.Text.IsEmpty; which returns me bool value. I dont want to compare for empty string every time. i.e. if(txtControl.text==strin

  • How can I add switch application functionality? 2010-05-06

    How can I add a Switch application menu item on a Screen? --------------Solutions------------- You'll need to set the DEFAULT_MENU style flag at instantiation. For example: import net.rim.device.api.ui.Screen; import net.rim.device.api.ui.container.F

  • how can I add a special function the android phone's volume button? 2011-06-12

    My question changed. can I do the same thing using volume button instead? (that is, trigger act2 even when the the phone is sleeping) Hi I want to write an app making use of the power button. The concept of the app is as follows Activity1 -->servi

  • Please add password reset functionality to domain 2014-05-16

    Hi there, I require the password reset functionality to be added to my custom domain Alistair. --------------Solutions------------- Hi, I have enabled the password reset option for your domain Thanks!

  • How can i add a toString() function for every class file, automated in Eclipse? 2010-01-28

    I'm doing a AS3 project in Eclipse and trace alot of values. I though it would be nice to have a toString() function in every class, at the bottom of each class as the last function, but i dont want to do this by hand for 500+ files. Is there a quick

  • How do I add a callback function to .submit() in Javascript? 2010-08-17

    I am submitting a page onclick of a form element and need a function to run after the submit refreshes the page. I'm trying to add an animated scroll back to the clicked element that caused the submission. I've got the scroll part covered but I can s

  • How do I add call a function in a dll in another project 2011-03-14

    I have a console C++ app and I want to call a function add() in another dll C++ project. Whats the way to do it? --------------Solutions------------- Use LoadLibrary and then GetProcAddress to get the function pointer. DLL files are accessed either b

  • How might I add a global function to jQuery .load -> .complete? 2012-04-25

    Writing a plugin for [top-secret] (not really :P) and I decided that one best course of action would be "IF" there is a definitive way to add function to ... maybe the $.ajax's complete func so that after a change is made, every new call to .load wil

  • How to dynamically add a Javascript function (and invoke) 2010-06-10

    Based on a click event on the page, via ajax I fetch a block of html and script, I am able to take the script element and append it to the head element, however WebKit based browsers are not treating it as script (ie. I cannot invoke a function decla

  • How do I add django-imaging functionality to an existing form on my project? 2011-05-30

    On my Django project I need to add images via ajax on one of my forms so I was investigating django-images. Reading the documentation is not very clear to me how do you actually use django-imaging on an existing project. I completed all the steps but

  • How do I add another validation function to an html button using javascript or jquery? 2011-11-08

    I have to use a user control form In that form, there is a Submit button. That button is html button. That button has lots of validation functions for the other controls. Now I want to add another function of my own into that button. Now I faced my p

  • How do you add a blog function to Plone 3? 2012-02-08

    We currently use Plone 3 for our training website and want to add a blog. Is this something that we can change or do we have to pay to add this function? --------------Solutions------------- was the be

  • How to implement "Add to favorites" function in android listview 2012-03-24

    I load data from internet to build the listview, and put a star icon for each row. If user checked the star, I would save the entire row data into sqllite databse. My question is that how can I know if one item has been added to favorites before to m

  • How do I add an onSelect functionality to a django-tables2 templateColumn dropdown list? 2013-09-25

    state_template = """ <select id="state_value"> <option{% if record.action_state = 'Planned' %} selected {% endif %}>Planned <option{% if record.action_state = 'In_Progress' %} selected {% endif %}>In Progress <option{% if record.

  • how do yo add the delete function in the message list. 2014-04-12

    converted from laptop to surface pro and now the delete function in the message list is no longer there. how do I correct this issue. --------------Solutions------------- Do you have the Touch bar enabled on the right side of the screen? When its ena

  • How do I get password reset function on 2014-06-03

    Hi, I manage a non-profit organization ( email that is hosted through I need to reset password for one of the users, however I don't see an option. How do I enable this feature? Thanks. --------------Solutions--------

Copyright (C), All Rights Reserved.

processed in 0.431 (s). 13 q(s)