How to update UILabel - objective-c

I have a UILabel that I would like to update. It has been added to the class by ctrl-cicking and adding through the XIB file. Im trying to update the label text after waiting for a short delay. As of now, there is nothing else going on except for the code below. Howver when I run this, the simulator blanks out for a moment and takes me straight to the last updated text. It doesnt show me the 100 just the 200.
How do I get the label to update like I want it to. Ultimately Im trying to have a timer of sorts decrement inside the label.
Label linked from the XIB to header file:
#property (strong, nonatomic) IBOutlet UILabel *timeRemainingLabel;
In Implmentation:
- (void)viewDidLoad
{
[super viewDidLoad];
self.timeRemainingLabel.text = #"100";
sleep(1);
self.timeRemainingLabel.text = #"200";
}
It has been synthesized.
XCode 4.3.2, Mac OSX 10.7.3, iOS Simulator 5.1 (running iPad), iOS 5

The problem with your implementation is that the execution sequence does not leave the method while in the sleep. This is the problem, because the UI subsystem never gets a chance to update the label to the "100" value before it gets a command to set it to "200".
To do this correctly, first you need to create a timer in your init method, like this:
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:#selector(updateLabel) userInfo:nil repeats: YES];
Then you need to write the code for your updateLabel method:
-(void) updateLabel {
NSInteger next = [timeRemainingLabel.text integerValue]-1;
timeRemainingLabel.text = [NSString stringWithFormat:#"%d", next];
}

It will never show you 100 like this because you are using sleep here which is stopping the execution of your program and just after 1 sec of sleep you are updating the text. If you want to do this then you can use an NSTimer for this.
Change your above code like this:
- (void)viewDidLoad
{
[super viewDidLoad];
self.timeRemainingLabel.text = #"100";
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(updateLabel) userInfo:nil repeats:NO];
}
- (void) updateLabel
{
self.timeRemainingLabel.text = #"200";
}

Your view doesn't appear till the view hasn't loaded and the text of the label timeRemainingLabel is #"200" when that happens. So you do not see the text changing. Use an NSTimer to do this instead and assign the text to the label in the selector:
timer = [NSTimer scheduledTimerWithTimeInterval:timeInSeconds target:self selector:#selector(updateText) userInfo:nil repeats: YES/NO];
and in your update method, set the latest text as per your requirement:
-(void) updateText {
self.timeRemainingLabel.text = latestTextForLabel;
}

Related

Update UILabel according to NSTimer [duplicate]

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];
}
}

NSTimer stopwatch with specific control

I have a label 00:00.0 and button. When I press button the timer should stars counting on label it's no matter how many clicks I'll do. The timer should stop when I tap on label, and should reset on second tap on same label.
I tried everything, but I can't reach result :) Anybody please help me.
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
IBOutlet UILabel *stopWatchLabel;
NSTimer *stopWatchTimer; // Store the timer that fires after a certain time
NSDate *startDate; // Stores the date of the click on the start button
}
#property (nonatomic, retain) IBOutlet UILabel *stopWatchLabel;
- (IBAction)onStartPressed:(id)sender;
- (IBAction)onStopPressed:(id)sender;
- (void)updateTimer;
-(IBAction)onStartpressed:(id)sender{
NSTimer * timer = [[NSTimer alloc] initWithFireDate:[NSDate date] interval:1.0 target:self selector:#selector(methodthatUpdateslabel) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
}
-(IBAction)onStopPressed:(id)sender{
[timer invalidate];
}
This should do it hope it helps.
Quoting some code inside my active project.
-(void)start{
[self setup];
if(_timer == nil){
_timer = [NSTimer scheduledTimerWithTimeInterval:kDefaultFireInterval target:self selector:#selector(updateLabel:) userInfo:nil repeats:YES];
_counting = YES;
}
}
-(void)pause{
[_timer invalidate];
_timer = nil;
_counting = NO;
}
That's the basic concept of implement a stopwatch, if you want to use UILabel as a stopwatch, MZTimerLabel is a perfect two line solution for you. Take a look.
Cheers.

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];
}
}

UIButton Touch and Hold

I haven't found a very easy way to do this. The ways I've seen require all these timers and stuff. Is there any easy way I can hold a UIButton and cause it to repeat the action over and over until it gets released?
You can do the following: Make an NSTimer that will start up when the app starts or in viewDidLoad and also make a boolean.
For example:
//Declare the timer, boolean and the needed IBActions in interface.
#interface className {
NSTimer * timer;
bool g;
}
-(IBAction)theTouchDown(id)sender;
-(IBAction)theTouchUpInside(id)sender;
-(IBAction)theTouchUpOutside(id)sender;
//Give the timer properties.
#property (nonatomic, retain) NSTimer * timer;
Now in your implementation file (.m):
//Synthesize the timer
#synthesize timer;
//When your view loads initialize the timer and boolean.
-(void)viewDidLoad {
g = false;
timer = [NSTimer scheduledTimerWithInterval: 1.0 target:self selector:#selector(targetMethod:) userInfo:nil repeats: YES];
}
Now make an IBAction for "Touch Down" set the boolean to lets say true. Then make another IBAction button for "Touch Up Inside" and "Touch Up Outside" assign the boolean to false.
For example:
-(IBAction)theTouchDown {
g = true;
}
-(IBAction)theTouchUpInside {
g = false;
}
-(IBAction)theTouchUpOutside {
g = false;
}
Then in that NSTimer method, put the following:(assume g is the boolean you have declared)
-(void) targetmethod:(id)sender {
if (g == true) {
//This is for "Touch and Hold"
}
else {
//This is for the person is off the button.
}
}
I hope this simplifies everything... I know it still uses a timer but there is not another way.
Unfortunately, it still looks like you have to code this functionality for yourself. simplest way (You still need a timer though):
A function that performs the action you want to repeat:
-(void) actionToRepeat:(NSTimer *)timer
{
NSLog(#"Action triggered");
}
in your .h file declare and set a property for a timer:
#interface ClassFoo
{
NSTimer* holdTimer;
}
Then in the .m make two IBActions:
-(IBAction) startAction: (id)sender
{
holdTimer = [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:#selector(actionToRepeat:) userInfo:nil repeats:YES];
[holdTimer retain];
}
-(IBAction) stopAction: (id)sender
{
[holdTimer invalidate];
[holdTimer release];
holdTimer = nil;
}
Then Just link to the Touch Down event in IB from the button to startAction and the Touch Up Inside to the 'Stop Action'. It isn't a one liner but it allows you to customise the rate the action repeats as well as allowing you to trigger it from another outlet/action.
You might consider subclassing UIButton and adding this functionality if you are going to be using this functionality often - then it is only (slightly) painful to implement the first time.
An other way to use this NBTouchAndHoldButton. This is exactly what you want, and very easy to implement it:
TouchAndHoldButton * pageDownButton = [TouchAndHoldButton buttonWithType:UIButtonTypeCustom];
[pageDownButton addTarget:self action:#selector(pageDownAction:) forTouchAndHoldControlEventWithTimeInterval:0.2];
Good luck!
I cannot reply to the first one, but this line:
timer = [NSTimer scheduledTimerWithInterval: 1.0 target:self selector:#selector(targetMethod:) userInfo:nil repeats: YES];
for at least iOS 4.1 and newer needs to be:
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:#selector(targetMethod:) userInfo:nil repeats: YES];
I know this is an old question, but as an easy way, like to consider using "[NSObject performSelector:withObject:afterDelay:]" to repeatedly invoke methods in any particular time interval.
In this case:
NSTimeInterval someTimeInterval = 1;
- (IBAction)action:(id)sender {
UIButton * const button = sender;
if (button.state != UIControlStateHighlighted) {
return;
}
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:_cmd object:sender];
[self performSelector:_cmd withObject:sender afterDelay:someTimeInterval];
}