nstimer

Stop and restart a timer

强颜欢笑 提交于 2019-12-05 07:21:24
问题 I want to stop this timer and then restart it from where I stopped it. secondsTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(addSeconds), userInfo: nil, repeats: true) Below, it was suggested I shouldn't increment a timer in my timer handler. Why not? For example, using GCD timer: func countSeconds() { secondsTimer = DispatchSource.makeTimerSource(queue: .main) secondsTimer?.schedule(deadline: .now(), repeating: 1.0) secondsTimer?.setEventHandler { [weak

iOS Countdown Timer To Specific Date

北城余情 提交于 2019-12-05 06:07:48
问题 I am trying to get a countdown timer to a specific date. I use: -(void) viewWillAppear:(BOOL)animated { NSString *str =@"12/27/2013"; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"MM/dd/yyyy"]; NSDate *date = [formatter dateFromString:str]; NSTimeInterval numberOfSecondsUntilSelectedDate = [date timeIntervalSinceNow]; NSInteger numberOfDays = numberOfSecondsUntilSelectedDate / 86400; startTime = [[NSDate date] retain]; secondsLeft = numberOfDays; NSLog

Set an NSTimer to fire once in the future

风格不统一 提交于 2019-12-05 05:37:14
How do I set up an NSTimer to fire once in the future (say, 30 seconds). So far, I have only managed to set it so it fires immediately, and then at intervals. The method you want to use is: + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval) seconds target:(id) target selector:(SEL) aSelector userInfo:(id) userInfo repeats:(BOOL) repeats with repeats == NO arguments and seconds == 30 . This will create the timer and schedule it. It will fire only once, in 30 seconds (and not immediately). JiteshW You can set the timer with your future date, and set repeats to NO + (NSTimer *

Thread and NSTimer

天大地大妈咪最大 提交于 2019-12-05 05:24:13
问题 I am making an application that has a timer. I count the minutes and seconds from a given time down to 0. When this happen I launch an alertview. My structure is this: Mainthread method allocate a new thread and initialize it. The entrypoint(method) for the thread has a timer which invokes a method for calculating time left, and if the time is up, displays an alertview. However, is this correct? Because now I am updating GUI from another thread than the main...and that is bad right? And I am

Invalidating an NSTimer in dealloc

匆匆过客 提交于 2019-12-05 02:58:44
问题 Following this question, and more specifically, this comment: because retain (aka strong reference) cycles in the common case where the timer's target is also its owner I am wondering why dealloc isn't a good place to invalidate an NSTimer . I remember profiling my app without auto-repeating NSTimer invalidation and then with invalidation in dealloc , and the memory correctly freed. Is dealloc working differently in the latest iOS? Isn't in fact your overridden dealloc called prior to any

NSTimer stops firing in Background after some time

◇◆丶佛笑我妖孽 提交于 2019-12-05 01:10:21
问题 Hey I am developing an app in which i have to make API call every 30 sec, so i created NSTimer for it. But when my app goes into background timer stops firing after 3-4 minutes. So it works only 3-4 minutes in background,but not after that. How can i modify my code so that timer would not stop. Here is some of my code. - (IBAction)didTapStart:(id)sender { NSLog(@"hey i m in the timer ..%@",[NSDate date]); [objTimer invalidate]; objTimer=nil; UIBackgroundTaskIdentifier bgTask =

Best method to get text to blink in iPhone OS?

此生再无相见时 提交于 2019-12-05 00:51:51
问题 I want my text box to blink (like the old LCD clocks). Right now, I'm calling a myriad of NSTimers and selectors that wait, change the alpha, wait, then change it back. Even with this, it looks really bad, and I'm thinking I have to put an NSTimer to gradually change the alpha, but from what I hear they are not meant for things of that precision. My thoughts are there must be a way to do this a lot better than how I am currently implementing it. It feels like hack. 回答1: Using an animation

UILabel Not Updating When Returning to UIViewController

二次信任 提交于 2019-12-04 19:57:05
I have a simple app that has an NSTimer object in the appDelegate to be accessed by all views. The structure of the app is with a UINavigationController. When I fire the NSTimer object, my UILabel is being updated with the correct countdown function, but when I go back to the rootViewController and back to the countdown timer view, my UILabel is being updated with the current countdown time, but no subsequent updates to the UILabel happen. What am I missing? I have done research on making sure the UILabel object is not nil, that I call the function on the viewDidAppear method, and nothing

NSTimer in background

笑着哭i 提交于 2019-12-04 19:23:44
I am very new in Iphone Development. I have an issue . I am using a NSTimer which update a UiLabel in every second. now i have two problem : when my app goes in background and after it when i open app . app goes hangs. if i goes next or back on other ui screen then wen i comes on timer screen then my label again shows 0. can anyone help me. code which i am using : timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES]; -(void) updateCountdown { secondsLeft--; //nits testing if(secondsLeft == 1) { [self.view addSubview

Suicide: Objective-C objects calling their own -dealloc methods on themselves

元气小坏坏 提交于 2019-12-04 19:11:57
Is it good practice for an object in Objective-C to commit suicide? That is, for an object to declare [self dealloc] where -dealloc permits an orderly wind down as usual? What are the principal risks? As it happens I have a specific example, a custom timer object that extends NSObject and comprises an NSTimer instance and an NSUInteger which is set to limit the number of times the timer fires. When time is up the object tells the timer to -invalidate and then commits suicide by calling its -dealloc method. As this is automatic we have no worries about having to track the object or crucially