nstimer

iPhone - NSTimer not repeating after fire

一笑奈何 提交于 2019-12-21 07:55:39
问题 I am creating and firing a NSTimer with: ncTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES]; [ncTimer fire]; AND - (void)handleTimer:(NSTimer *)chkTimer { // do stuff } I am retaining my timer with: @property (nonatomic, retain) NSTimer *ncTimer; For some reason the timer is not repeating. It is firing once only and than never again. 回答1: You can't just assign to the timer that you have put as a property in your header.

Better alternative for NSTimer

二次信任 提交于 2019-12-21 06:33:09
问题 I'm just wondering, is there a better, more efficient function to use than NSTimer? I have a NSTimer code like this on my ViewDidLoad function: [NSTimer scheduledTimerWithTimeInterval:900.0f target:self selector:@selector(methodToRun) userInfo:nil repeats:YES]; With the function methodToRun: -(void)methodToRun { if(currentTime == presetTime) { //do something } } This works fine but the problem is, this eats up a lot of memory and I am getting memory warnings. So, what's a better, more

How to find if NSTimer is active or not?

感情迁移 提交于 2019-12-21 03:24:16
问题 I have a something like this: NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats:YES]; I am updating a label's text using this timer. Not at a certain condition I want to check if timer is active then invalidate the timer. My question is how do I find that timer is active or not? 回答1: When a non repeating timer fires it marks itself as invalid so you can check whether it is still valid before cancelling it (and of

How to parse and extract Year, Month, Day etc from time interval on iphone IOS

匆匆过客 提交于 2019-12-21 02:51:49
问题 I am slowly getting into iOS development and trying to create a count up timer from a specific date. I have figured out the code which gives me the interval in seconds but I could not figure out how to extract Year/Month/Day/Hour/Minute/Second values from it in order to display each value in its own label as a ticker. So far I have figured out that the following will give me the interval between the 2 dates in seconds, what I am trying to do is to parse this and display on my view this as a

NSTimer disables dealloc in UIView

Deadly 提交于 2019-12-20 12:38:48
问题 @interface someview:UIView{ NSTimer* timer; } @end @implementation someview -(void)dealloc{ NSLog(@"dealloc someview"); [timer invalidate]; timer = nil; } -(void)runTimer{ // } -(void)someMethod{ timer = [NSTimer timerWithTimeInterval:2.0f target:self selector:@selector(runTimer) userInfo:nil repeats:YES]; } @end Releasing someview will NOT call dealloc and the timer keeps on running. If I comment out the "timer = [NSTimer schedule...." part, dealloc will be called. Which means all the other

IOS: stop a NSTimer [duplicate]

这一生的挚爱 提交于 2019-12-20 11:56:05
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: NSTimer doesn't stop I have this code: [NSTimer scheduledTimerWithTimeInterval:110.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats:YES]; - (void) targetMethod:(NSTimer) timer{ NSLog(@"Hello World");} in targetMethod I can stop timer with [timer invalidate], but out of this method, How can I stop targetMethod? 回答1: You can keep your NSTimer in a variable and stop the timer using the

Having a NSTimer running background

北城余情 提交于 2019-12-20 07:52:47
问题 I've seen hundreds of solutions of how to get a NSTimer to run in the background. I know that it is possible, just look at apps like Strava and Runkepper that tracks your time when working out. But what is the best practice solution for doing so? I can't find one unison solution for this. Also, I would like the NSTimer to be used across different UIViewControllers. How is this done as a best practice? Thanks in regards! :) 回答1: NSTimers don't run in the background. Store the current time and

Using NSTimer in a Loop

僤鯓⒐⒋嵵緔 提交于 2019-12-20 07:14:29
问题 I need to show a timer counting down from 30 to 0, several times. (30 to 0, start over at 30, etc) However when I placed it in a for-loop, instead of waiting till the timer invalidates to begin the next timer, the loop iterates through and several timers are created. How can I form a loop that waits until the timer has invalidated? - (IBAction)startCountdown:(id)sender { NSNumber *rounds = [[NSNumber alloc]initWithInt:sliderRounds.value]; for (int i = rounds.intValue; i > 0; i--) { [self

How do I have one timer countdown and the other randomly move an object?

大城市里の小女人 提交于 2019-12-20 06:25:13
问题 I have the right code to move an object randomly and also to make numbers count down but I am not able to have an object move while the timer counts down. One thing I found weird though was that when I took out the part about the TimeLeft label the objects moved randomly but obviously the numbers did not count down. The main question is: how can I have a timer countdown and have the object move at the same time? (my main goal is to make the object stop moving once the timer reaches zero) I

How to use NSTimer to fire off multiple times?

ⅰ亾dé卋堺 提交于 2019-12-20 05:53:34
问题 I have a RNG and want it to go off every three seconds. So far I have var timer = NSTimer(timeInterval: 3, target: self , selector: randomnumbers, userInfo: nil, repeats: true) func randomnumbers() { var rockNamesArray:[String] = ["bird", "rock2", "rock3"] var rockpos = Int(arc4random_uniform(UInt32(3))) } But I have a bunch of error messages and I'm not sure how to organize it. EDIT The error message in this code is telling me that it has an unresolved identifier "self" and all the other