nstimer

NSTimer in background

可紊 提交于 2019-12-06 15:02: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]; -

UILabel Not Updating When Returning to UIViewController

天涯浪子 提交于 2019-12-06 14:28:22
问题 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

Pause NSTimer on home-button-press and start them again once the app is in foreground

社会主义新天地 提交于 2019-12-06 14:20:06
I've been searching for a solution to pause my SpriteKit game when the user "tabs down" the game. So far I found a solution where you use SKAction 's instead of NSTimer 's, this works as long as the time between actions stays the same. However, my NSTimer 's changes in speed. So I need to find another solution. I have a bunch of NSTimer 's located in GameScene -> didMoveToView NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: Selector("SpawnBullets"), userInfo: nil, repeats: true) NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("SpawnMeteors"),

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

谁说胖子不能爱 提交于 2019-12-06 14:14:25
问题 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

Adding pause functionality for NSTimer

我的未来我决定 提交于 2019-12-06 12:47:48
问题 First, here is some working code for a stopwatch in Xcode. I got two buttons, Start and Stop . Their titles change when the buttons are pressed. Now I want to add a pause functionality. I know that there are many threads about this, but (I don't know why) I was not able to get it working. So what is the best approach to implement this function in my code ? I already tried to use a pause date and subtract it from my NSTimeInterval but got negative values ... Thanks so far! So I did this: //use

Proper way of passing a primitive argument with an NSTimer

江枫思渺然 提交于 2019-12-06 12:07:00
I'm using a basic timer that calls this method: - (void) refresh:(id)obj { if (obj == YES) doSomething; } I want to call this method from certain areas of my code and also from a timer [NSTimer scheduledTimerWithTimeInterval:refreshInterval target:self selector:@selector(refresh:) userInfo:nil repeats:YES]; When I put YES as the argument for the userInfo parameter, I get an EXC_BAD_ACCESS error; why is this? Can someone help me do this the right way so that there is no ugly casting and such? The userInfo parameter must be an object; it is typed id . YES is a primitive, namely the value 1 . In

iOS NSTimer 定时器用法总结

浪尽此生 提交于 2019-12-06 11:18:43
NSTimer在IOS开发中会经常用到,尤其是小型游戏,然而对于初学者时常会注意不到其中的内存释放问题,将其基本用法总结如下: 一、初始化方法:有五种初始化方法,分别是 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo; - (void)viewDidLoad { [super viewDidLoad]; //初始化一个Invocation对象 NSInvocation * invo = [NSInvocation invocationWithMethodSignature:[[self class] instanceMethodSignatureForSelector:@selector(init)]]; [invo setTarget:self]; [invo setSelector:@selector(myLog)]; NSTimer * timer = [NSTimer timerWithTimeInterval:1 invocation:invo repeats:YES]; //加入主循环池中 [[NSRunLoop mainRunLoop]addTimer:timer forMode

How to create an alarm clock app with swift?

梦想与她 提交于 2019-12-06 07:49:51
问题 I'm trying to create a kind of alarm clock app with the swift but I could not figure out how to set an alarm model. I've tried UILocalnotification but I don't want my users to be involved the flow of alarm app other than setting the alarm. Then tried NSTimer and NSRunloop etc. but that didn't work either since those don't work when app gets background. So is there any alternative ways to do that? Any help and suggestion would be appreciated, thanks. 回答1: I have been doing research on this for

Keeping a timer running on another page when navigating around

跟風遠走 提交于 2019-12-06 07:40:10
I'm trying to keep a timer running on another page when you switch to other pages and complete other tasks, in essence keeping a clock on how long it takes to do the tasks. Whenever I switch to another page, it resets the timer back to what it was started, and does the same with some switches on other pages that I'm trying to keep on. Any ideas? Screenshot of storyboards: Code so far: // // ViewController.m #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (IBAction)start{ ticker = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self ``selector:

Call a method with delay of 15 sec irrespective of app state

此生再无相见时 提交于 2019-12-06 07:19:38
I need to call a method in every 15 seconds irrespective of any fact, whether it is on any view controller in foreground , whether it is in background or it is killed , I need to call it at all times. I know I can do the delay task using NSTimer NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval: 15.0 target: self selector: @selector(callAfterFifteenSeconds:) userInfo: nil repeats: YES]; But, I wanted to know where to implement it so that it could fulfil my condition. I guess I can use it in App Delegate but I need a guidance for this to implement it correctly. Calling it in App