nstimer

NSTimer inside custom tableViewCell

戏子无情 提交于 2019-12-02 04:15:22
I'm activating a function in my custom cell class from a viewController. The custom cell class looks like this: import UIKit class TableViewCell: UITableViewCell { var counter = 10 class func timerStarted(){ var timer = NSTimer() timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "update", userInfo: nil, repeats: true) } class func update(){ let cell = TableViewCell() var count = cell.counter count = --count println(counter) } } The problem is that the variable counter does not change, so 9 is printed every interval. How do I make it so that it changes value every time

Swift: How to invalidate a timer if the timer starts from a function?

和自甴很熟 提交于 2019-12-02 01:43:48
问题 I have a timer variable in a function like this: timer = NSTimer() func whatever() { timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timerbuiltingo", userInfo: nil, repeats: true) } when I try to stop the timer in the resulting timerbuiltingo function like this: func timerbuiltingo() { timer.invalidate() self.timer.invalidate() } It doesn't stop it. How should I be doing this? 回答1: If you need to be able to stop the timer at any point in time, make it an instance

Timer that updates label running in background

别说谁变了你拦得住时间么 提交于 2019-12-02 01:25:00
I have coded a timer using an NSTimer that updates a label. The problem is that in the same viewcontroller I have a uitableview and when I scroll it down, the timer doesn't update its value so the user can "cheat" to stop the timer. I think this could be easy to fix with a serial queue with CGD but I don't figure out how to do it. Thanks in advance, Juan First of all keep in mind that you cannot perform UI changes in any other thread than the main thread. Having said that, you need the NSTimer to fire in the main queue, otherwise the program will crash when changing the UILabel . Have a look

Update a label with speed every x seconds

旧时模样 提交于 2019-12-01 21:53:11
问题 I'm developing my first iPhone application. I have to update a label with the device speed every x seconds. I have created my own CLController and I can get device speed but I don't know if I have got to use NSTimer to update my label. How can I do it? 回答1: You can schedule the timer like this NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:YOUR_INTERVAL target:self selector:@selector(updateLabel) userInfo:nil repeats:YES]; Now below method will get called in every YOUR_INTERVAL

Update a label with speed every x seconds

若如初见. 提交于 2019-12-01 21:00:33
I'm developing my first iPhone application. I have to update a label with the device speed every x seconds. I have created my own CLController and I can get device speed but I don't know if I have got to use NSTimer to update my label. How can I do it? You can schedule the timer like this NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:YOUR_INTERVAL target:self selector:@selector(updateLabel) userInfo:nil repeats:YES]; Now below method will get called in every YOUR_INTERVAL (in seconds) periods - (void) updateLabel { myLabel.text = @"updated text"; } To stop the timer you could call

Correct way to release NSTimer?

依然范特西╮ 提交于 2019-12-01 18:51:53
What is the correct way to release a NSTimer in my dealloc method ? It was created with the following code ? -(void)mainTimerLoop { mainTimer = [NSTimer scheduledTimerWithTimeInterval:1/10 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES]; } Thanks JonLOo you have a really good answer about NSTimer here How do I use NSTimer? there they talk about stoping a repeating NSTimer doing [myTimer invalidate]; The way you're doing it, you won't ever hit dealloc . A timer retains its target. In this case, that means the timer has retained you. It will not release you until it is

Stop and Reset NSTimer

你。 提交于 2019-12-01 18:37:23
I have a simple timer using that is activated with a button is pushed. It runs from 60 to 0 no problem but what I want is to stop and reset the timer on push button. I have managed to stop it when the button is pressed using the code below but for some reason cannot get it to reset and stop at 60. This should be simple but it isn't working. Any suggestions? Timer is set using simple Action - (IBAction)timerStart:(id)sender { if(!secondCounter == 0){ [countdownTimer invalidate]; } else { [self setTimer]; } } CODE FOR TIMER - (void)timerRun { secondCounter = secondCounter - 1; int minutes =

Timer decrement gets varied for each page load

岁酱吖の 提交于 2019-12-01 14:37:59
I have a timer for my project, each time it decrements by 1 second. But if the counter starts working for the second time it gets decremented by 2 seconds and for the third time by 3 seconds etc. what should I do to get 1 sec decrement for all the time? -(void)viewDidAppear:(BOOL)animated { count=15; //timer set as 15 seconds [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:)userInfo:nil repeats:YES]; //for decrementing timer } - (void)updateCounter:(NSTimer *)theTimer { count -= 1; NSString *s = [[NSString alloc] initWithFormat:@"%d", count]; if(count=

UILabel memory leak?

社会主义新天地 提交于 2019-12-01 14:25:08
I have an NSTimer that fires off every second, and on that second I update a UILabel by setting the text property like so: remainglbl.text = [NSString stringWithFormat:@"%i:%02i", var1, var2]; It works fine, but when I run it in xcode with Start With Performance Tool -> Leaks, it appears that the memory just keeps on climbing and climbing and climbing. From my understanding, the string should be autoreleased (although I never see the memory decrease, or stop increasing). Is this a memory leak? Is there a better way I can do this to keep my memory usage in check? Thanks! Update: code to create

UILabel memory leak?

£可爱£侵袭症+ 提交于 2019-12-01 13:19:55
问题 I have an NSTimer that fires off every second, and on that second I update a UILabel by setting the text property like so: remainglbl.text = [NSString stringWithFormat:@"%i:%02i", var1, var2]; It works fine, but when I run it in xcode with Start With Performance Tool -> Leaks, it appears that the memory just keeps on climbing and climbing and climbing. From my understanding, the string should be autoreleased (although I never see the memory decrease, or stop increasing). Is this a memory leak