Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, if you know how to update a label by clicking a button, you have everything in place to connect everything else:</p> <p>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 <code>advanceTimer</code> method?</p> <p>Yet easier, you could connect your timer class to the label. You might do it like this:</p> <pre><code>// 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 &lt;= 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 </code></pre> <p>This code has a two benefits:</p> <ul> <li>You can cancel your timer.</li> <li>Your view controller does not need to know anything about this — you can set this up in interface builder, entirely.</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload