nainsi gupta
nainsi gupta

Reputation: 163

My Timer Not Reset and Not Stop

I want to make Stop watch . On clicking on Start Button My timer is Start button on Click Stop my timer not stop. And on clicking Reset My timer reset then start again it is not right behaviour. Please help. I am newer in swift. Any help would be apperciated.Thamks in Advance

 class ViewController: UIViewController {
        @IBOutlet var label:UILabel?
        var time: Int!
        var appTimer :NSTimer = NSTimer()
        override func viewDidLoad() {
            super.viewDidLoad()
           time = 0
            label?.text = self.getFormattedString()
            // Do any additional setup after loading the view.
        }

    }
    extension ViewController
    {
        func getFormattedString() ->NSString
        {

            var t :Int = time!
            var minQuotient :Int = time/600
            var  minRemainder:Int = time%600
            var secQuotient:Int = minRemainder/10
            var secRemainder:Int = minRemainder%10
            var  minString :NSString = NSString(format: "%d", minQuotient)
            var secString :NSString = NSString(format: "%d", secQuotient)
            if secQuotient < 10
            {
                secString = NSString(format: "0%@", secString)

            }
            var secRemainderString :NSString = NSString(format: "%d", secRemainder)
            return NSString(format: "%@:%@.%@",minString,secString,secRemainderString)



        }




        @IBAction func statrt()
        {
           appTimer.invalidate()
            var timer :NSTimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector:Selector("stopWatch:") , userInfo: nil, repeats: true)
        }

        func stopWatch (theTimer:NSTimer)
        {
            time = time+1
            var b : String = self.getFormattedString()
            label?.text = b
        }



        @IBAction func stop()
        {
             self.appTimer.invalidate()
        }


        @IBAction func reset()
        {
             self.appTimer.invalidate()
           time = 0
            label!.text = self.getFormattedString()
        }
    }

Upvotes: 2

Views: 258

Answers (3)

gnasher729
gnasher729

Reputation: 52530

You are confusing yourself between instance variables and plain method variables.

You don't always want a timer, so appTimer should be optional and nil, where you pointlessly create a timer. stop method invalidates any existing timer and sets the optional to nil. start method calls stop, then creates a new scheduled timer. And your deinit method calls stop.

Upvotes: 1

Wain
Wain

Reputation: 119021

Your timer instance variable should really be optional and not initialised to an invalid timer instance:

var appTimer :NSTimer?

Then, when you create your new timer instance you need to store it into that instance variable instead of just creating it as a local variable in the function:

appTimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector:Selector("stopWatch:") , userInfo: nil, repeats: true)

I would also recommend that you try to be consistent about using self. so you know what you're doing in relation to instance and local state management.

Upvotes: 1

Prabhu.Somasundaram
Prabhu.Somasundaram

Reputation: 1390

func start() {
    print("Fired")
    self.myTimer.invalidate()

    let ntimer :NSTimer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector:Selector("stopWatch") , userInfo: nil, repeats: true)
    NSRunLoop.mainRunLoop().addTimer(ntimer, forMode: NSDefaultRunLoopMode)
}

func stopWatch() {
    print("stopped")
}

In console this was printing Fired stopped stopped stopped you should find a place in your code stop the second timer.

Upvotes: 0

Related Questions