Update UILabel according to NSTimer [duplicate] - objective-c

This question already has answers here:
Objective-C : NSTimer and countdown
(2 answers)
Closed 9 years ago.
I have a UILabel that I would like to update as a countdown timer. Currently I am using an NSTimer to execute a method when the allotted inactivity time has passed. I found the code for setting up the desired NSTimer from this SO thread. I'm using the example code posted by Chris Miles in one of the view controllers for the application, and the method is executing properly when the idle time reaches the kMaxIdleTimeSeconds.
However I was hoping to take the code example posted by Chris Miles a step further by updating a UILabel in the view controller with the remaining idle time. Should I use a completely separate NSTimer to do this, or is there a way to update UILabel with idle time remaining with the current NSTimer before logout?
The view controller implementation file for the application looks like the following,
#import "ViewControllerCreate.h"
#import "math.h"
#interface ViewControllerHome ()
#define kMaxIdleTimeSeconds 20.0
#implementation ViewControllerHome
#end
- (void)viewDidLoad
{
// 5AUG13 - idle time logout
[self resetIdleTimer];
int idleTimerTime_int;
idleTimerTime_int = (int)roundf(kMaxIdleTimeSeconds);
_idleTimerTime.text = [NSString stringWithFormat:#"%d secs til",idleTimerTime_int];
}
- (void)viewDidUnload
{
[self setIdleTimerTime:nil];
// set the idleTimer to nil so the idleTimer doesn't tick away on the welcome screen.
idleTimer = nil;
[super viewDidUnload];
}
#pragma mark -
#pragma mark Handling idle timeout
- (void)resetIdleTimer {
if (!idleTimer ) {
idleTimer = [NSTimer scheduledTimerWithTimeInterval:kMaxIdleTimeSeconds
target:self
selector:#selector(idleTimerExceeded)
userInfo:nil
repeats:YES];
}
else {
if(fabs([idleTimer.fireDate timeIntervalSinceNow]) < kMaxIdleTimeSeconds-1.0) {
[idleTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:kMaxIdleTimeSeconds]];
}
}
}
- (void)idleTimerExceeded {
NSLog(#"lets see what happens");
[idleTimer invalidate];
[self logout:nil];
[self resetIdleTimer];
}
// method is fired when user touches screen.
- (UIResponder *)nextResponder {
[self resetIdleTimer];
return [super nextResponder];
}
#end

I wouldn't use the code you posted at all. Why not start the label with the max idle time, then call the timer's action method once every second, and subtract 1 from the label's text's intValue. When the label's value reaches 0, do what ever you need to do, and invalidate the timer.
Something like this:
- (void)viewDidLoad {
[super viewDidLoad];
self.label.text = #"1000";
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(countDown:) userInfo:nil repeats:YES];
}
-(void)countDown:(NSTimer *) aTimer {
self.label.text = [NSString stringWithFormat:#"%d",[self.label.text intValue] - 1];
if ([self.label.text isEqualToString:#"0"]) {
//do whatever
[aTimer invalidate];
}
}

Related

How do I set a NSTimer to stop if a certain action is running?

I have a popup in a custom view controller that is presented after 1 minute thanks to my NSTimer.
My NSTimer code:
[NSTimer scheduledTimerWithTimeInterval:60.0f
target:self selector:#selector(methodB:) userInfo:nil repeats:YES];`
The method that reveals the popup
- (void) methodB:(NSTimer *)timer
{
//Do calculations.
[self showPopupWithStyle:CNPPopupStyleFullscreen];
}
I'm trying to set the NSTimer to stop running if the [self showPopupWithStyle:CNPPopupStyleFullscreen]; is currently open, running or active.
Then I would like to start the NSTimer back up again if the popup view controller is NOT open, running or active.
Any help, samples or examples would be greatly appreciated!
My project is written in Objective-C.
EDIT:
I tried the answers in the suggested "possibly similar" answer and it doesn't seem to work for what I am doing. How do I stop NSTimer?
This seems to do the trick.
#property (nonatomic, strong) CNPPopupController *popupController;
- (void)_timerFired:(NSTimer *)timer;
#end
NSTimer *_timer;
- (void)viewDidLoad {
[super viewDidLoad];
if (!_timer) {
_timer = [NSTimer scheduledTimerWithTimeInterval:10.0f
target:self
selector:#selector(_timerFired:)
userInfo:nil
repeats:YES];
}
}
- (void)_timerFired:(NSTimer *)timer {
if ([_timer isValid]) {
[self showPopupWithStyle:CNPPopupStyleFullscreen];
[_timer invalidate];
}
_timer = nil;
NSLog(#"ping");
}
If you want to restart the timer just add this code where you'd like the action to start back up.
if (!_timer) {
_timer = [NSTimer scheduledTimerWithTimeInterval:10.0f
target:self
selector:#selector(_timerFired:)
userInfo:nil
repeats:YES];
}
Hope this helps! :)

Touches are getting noticed twice in objective c

I am implementing session inactivity for my app so that if user is inactive for 30 seconds, then show him a new uiviewcontroller as a formsheet. For touch event, i am using this code
(void)sendEvent:(UIEvent *)event {
[super sendEvent:event];
// Only want to reset the timer on a Began touch or an Ended touch, to reduce the number of timer resets.
NSSet *allTouches = [event allTouches];
if ([allTouches count] > 0) {
// allTouches count only ever seems to be 1, so anyObject works here.
UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded) {
[[BCDTimeManager sharedTimerInstance]resetIdleTimer];
}
}
}
In BCDTimeManager class which is a singleton class i have implemented resetIdleTimer and idleTimerExceed method
#import "BCDTimeManager.h"
#implementation BCDTimeManager
__strong static BCDTimeManager *sharedTimerInstance = nil;
NSTimer *idleTimer;
NSTimeInterval timeinterval;
+ (BCDTimeManager*)sharedTimerInstance
{
static dispatch_once_t predicate = 0;
dispatch_once(&predicate, ^{
sharedTimerInstance = [[self alloc] init];
NSString *timeout = [[NSUserDefaults standardUserDefaults] valueForKey:#"session_timeout_preference"];
timeinterval = [timeout doubleValue];
});
return sharedTimerInstance;
}
- (void)resetIdleTimer {
if (idleTimer) {
[idleTimer invalidate];
}
idleTimer = nil;
NSLog(#"timeout is %ld",(long)timeinterval);
idleTimer = [NSTimer scheduledTimerWithTimeInterval:timeinterval target:self selector:#selector(idleTimerExceeded) userInfo:nil repeats:true];
}
- (void)idleTimerExceeded {
NSLog(#"idle time exceeded");
[[NSNotificationCenter defaultCenter]
postNotificationName:#"ApplicationTimeout" object:nil];
}
But when i do any touch on the screens, in console, i can see NSLog is printed twice which is causing my NSNOtification action to be triggered twice.
I am not sure what i am doing wrong. Please help me to figure out this.
I figured it out. Code is doing right. I am seeing NSLog twice because of two touch event one touch began and one touch ended. So, this code is correct without any issue. Something is wrong with observers add or remove method. I will look into that

Where do I put my loop?

I really need some help on where to put this loop:
int timer = 10;
while (timer >= 0) {
[secondsLeft setText:[NSString stringWithFormat:#"%d", timer]];
NSLog(#"%d", timer);
timer--;
sleep(1);
}
Anyways, wherever I put this loop I get some sort of error except for under the IBAction where it works perfectly except it delays the button press by 10 seconds :P
Here's my .m file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize answer;
#synthesize secondsLeft;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
}
- (IBAction)answerButton:(id)sender {
NSString *str = answer.text; // Takes user input from answer.text
int answerOne = [str intValue]; // converts answer into an integer
if(answerOne == 30) {
self.secondsLeftToAnswer.text = #"Correct!";
} else {
self.secondsLeftToAnswer.text = #"You Suck at Math!";
}
}
#end
Anyways can someone please tell me how I can implement this loop into my code so that the loop displays its output to a UILabel (secondsLeft is the UILabel, loop is supposed to display a countdown from 10 to 0 in the UILabel) ?
EDIT: How would I go about implementing a NSTimer to do what I want (countdown from 10)? I tried to set one up but they are so confusing. Thanks for the help so far Jasarien!
The answer is "nowhere". Cocoa is an event-driven system and such loops stop the main thread from processing events and you won't see the text of the label change and user-interaction will be disabled. See the Main Event Loop documentation for details.
Instead use an NSTimer (for example), which works with the runloop to allow periodic method invocation.
In order to show countdown you need to update the label after specific time.
Below is the code depicting the functionality you want.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
count = 10;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target: self
selector: #selector(handleTimer)
userInfo: nil
repeats: YES];
}
-(void)handleTimer{
if (count >= 0) {
[lblTimer setText:[NSString stringWithFormat:#"%ld",(long)count]];
count--;
}else{
[timer invalidate];
}
}
Hope this will help you. Happy coding :)

NSTimer within a class

I have a timer class set up that is basically handling all of count down timer logic. All it does is on button press - counts from 60 to 0 seconds.
I have the following code in a Timer.m class.
- (void)advanceTimer
{
self.lengthOfTime = [NSNumber numberWithInt:self.lengthOfTime.intValue - 1];
NSLog(#"%#",self.lengthOfTime);
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(advanceTimer) userInfo:nil repeats:NO];
}
- (void)startCountdown
{
if (!self.lengthOfTime) self.lengthOfTime = [NSNumber numberWithInt:60];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(advanceTimer) userInfo:nil repeats:NO];
}
What I am looking to do is create a timer object in my View Controller that will update a label from the ViewController.m. Currently - the class works because I can NSLog from the Timer class and it counts down correctly. I thought about having the advanceTimer method return - but I can't seem to wrap my head around how to update the label in the ViewController with the returned data.
The only way I cold get the return to work was to have a button that refreshed the label to the correct countdown time... I can't get it to automatically count down...
Well, if you know how to update a label by clicking a button, you have everything in place to connect everything else:
If your view controller has an IBOutlet for the label and an IBAction that updates it, why not call the view-controller's action in your advanceTimer method?
Yet easier, you could connect your timer class to the label.
You might do it like this:
// Timer.h:
#interface Timer : NSObject
#property (retain, nonatomic) IBOutlet UILabel *timeLabel;
#property (assign, nonatomic) NSInteger secondsRemaining;
#property (assign, nonatomic) NSTimer *timer;
- (IBAction)startCountdown:(id)sender;
- (IBAction)stopCountdown:(id)sender;
- (void)timerFired:(NSTimer *)timer;
#end
// Timer.m
#implementation Timer
#synthesize timeLabel = timeLabel_;
#synthesize secondsRemaining = secondsRemaining;
#synthesize timer = timer_;
- (void)setTimer:(NSTimer *)timer
{
if (timer = timer_)
return;
[timer_ invalidate];
timer_ = timer;
}
- (void)scheduleTimer
{
if (self.secondsRemaining <= 0) {
self.timer = nil;
} else {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(timerFired:) userInfo:nil repeats:NO];
}
}
- (void)timerFired:(NSTimer *)timer
{
self.secondsRemaining -= 1;
NSString *displayString = [NSString stringWithFormat:#"%d", self.secondsRemaining];
self.timeLabel.text = displayString;
[self scheduleTimer];
}
- (IBAction)startCountdown:(id)sender
{
self.secondsRemaining = 60;
[self scheduleTimer];
}
- (IBAction)stopCountdown:(id)sender
{
self.timer = nil;
}
- (void)dealloc
{
[timeLabel_ release];
[super dealloc];
}
#end
This code has a two benefits:
You can cancel your timer.
Your view controller does not need to know anything about this — you can set this up in interface builder, entirely.
Correct me if I'm wrong, but I think you should retain that NSTimer in a class member. Otherwise the timer is destroyed when finishing the method.
#property (nonatomic,retain) NSTimer * yourTimer;
In the .m file
#synthesize yourTimer;
And then
- (void)advanceTimer
{
self.lengthOfTime = [NSNumber numberWithInt:self.lengthOfTime.intValue - 1];
NSLog(#"%#",self.lengthOfTime);
self.yourTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(advanceTimer) userInfo:nil repeats:NO];
}
- (void)startCountdown
{
if (!self.lengthOfTime) self.lengthOfTime = [NSNumber numberWithInt:60];
self.yourTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(advanceTimer) userInfo:nil repeats:NO];
}
I fixed also your NSNumber alloc, so there are no memory leaks ;)
Explaining it, NSTimer scheduledTimerWithTimeInterval: gives you an NSTimer with autorelease. If this Timer is not retained by some member, it gets released as soon as the method ends and, since no other pointer is retaining it, it gets freed. Maybe that's the explanation ;). Never worked with NSTimers
The countdown class needs to save a link back to the ViewController and then call a method on it.
One approach would be to use the delegate pattern. Have the Countdown class's init method as initWithDelegate:(id)delegate and a predefined callback method (like updateCountdown:(NSNumber*)currentCountdown). The ViewController sends itself as the delegate and implements the update method.
Another approach is the target/action pattern. NSTimer uses this approach. The init method would be initWithTarget:(id)target selector:(SEL)selector. The ViewController sends itself as the target and whatever selector it wants to use (as long as it takes an NSNumber as it's sole argument).
In both cases in advanceTimer the Countdown class will use performSelector:withObject: to call the ViewController's update method.
If you really want a true one second timer then set it to repeat. Otherwise you will drift slowly by the amount of time the advanceTimer method takes to fire and complete. At the end of the countdown use a reference to the timer to invalidate it.

Objective C & iOS: running a timer? NSTimer/Threads/NSDate/etc

I am working on my first iOS app, and have run in the first snag I have not been able to find a good answer for.
The problem: I have a custom UIGestureRecognizer and have it all wired up correctly, and I can run code for each touch in the #selector after recognition. This has been fine for most things, but it's a little too much input for others.
My goal: To make a timer that triggers at a specified interval to run the logic, and to be able to cancel this at the moment touches are cancelled.
Why I am asking here: There are a lot of possibilities for solutions, but none has stood out as the best to implement. So far it seems like
performSelector (and some variations on this)
NSThread
NSTimer
NSDate
Operation Queues
I think I found some others as well...
From all the research, some form of making a thread seems the route to go, but I am at a loss at which would work best for this situation.
An example of an implementation: an NSPoint is taken every 0.10 seconds, and the distance between the previous and current point is taken. [Taking the distance between every point was yielding very messy results].
The relevant code:
- (void)viewDidLoad {
CUIVerticalSwipeHold *vSwipe =
[[CUIVerticalSwipeHold alloc]
initWithTarget:self
action:#selector(touchHoldMove:)];
[self.view addGestureRecognizer:vSwipe];
[vSwipe requireGestureRecognizerToFail:doubleTap];
}
...
- (IBAction)touchHoldMove:(UIGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
}
if (sender.state == UIGestureRecognizerStateBegan) {
}
//other stuff to do goes here
}
Use an NSTimer
Set it up like this:
theTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(yourMethodThatYouWantRunEachTimeTheTimerFires) userInfo:nil repeats:YES];
Then when you want to cancel it, do something like this:
if ([theTimer isValid])
{
[theTimer invalidate];
}
Note that in the above example you would need to declare the "theTimer" instance of NSTimer where it will be available to both methods. In the above example the "0.5" means that the timer will fire twice a second. Adjust as needed.
For the sake of completeness, I am adding my final implementation here (not sure this is the way to do it, but here goes)
.h
#interface {
NSTimer *myTimer;
}
#property (nonatomic, retain) NSTimer *myTimer;
.m
#synthesize myTimer;
-------------------------------------------
- (void)viewDidLoad {
//Relevant snipet
CUIVerticalSwipeHold *vSwipe =
[[CUIVerticalSwipeHold alloc]
initWithTarget:self
action:#selector(touchHoldMove:)];
[self.view addGestureRecognizer:vSwipe];
[vSwipe requireGestureRecognizerToFail:doubleTap];
}
-------------------------------------------
- (IBAction)touchHoldMove:(UIGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
//Cancel the timer when the gesture ends
if ([myTimer isValid])
{
[myTimer invalidate];
}
}
}
if (sender.state == UIGestureRecognizerStateBegan) {
//starting the timer when the gesture begins
myTimer = [NSTimer scheduledTimerWithTimeInterval:someTimeIncrement
target:self
selector:#selector(someSelector)
userInfo:nil
repeats:YES];
}
}