-1

I am having trouble with NSTimer and adding a stopwatch to the Notification Centre; I am very new to Obj C and am not using Xcode. The Widget compiles and runs fine, however pushing "Start:" does nothing and there is no 00.00.00.

#import "BBWeeAppController-Protocol.h"
#import "UIKit/UIKit.h"

static NSBundle *_NCTimerWeeAppBundle = nil;

@interface NCTimerController: NSObject <BBWeeAppController> {
    UIView *_view;
    UIImageView *_backgroundView;
    UILabel *stopWatchLabel;
}
@property (nonatomic, retain) UIView *view;
@property (nonatomic, retain) NSTimer *stopWatchTimer;
@property (nonatomic, retain) NSDate *startDate;
@property (nonatomic, retain) UILabel *stopWatchLabel;
@end

@implementation NCTimerController
@synthesize view = _view;
@synthesize stopWatchTimer = _stopWatchTimer;
@synthesize startDate = _startDate;
@synthesize stopWatchLabel = _stopWatchLabel;

+ (void)initialize {
    _NCTimerWeeAppBundle = [[NSBundle bundleForClass:[self class]] retain];
}

- (id)init {
    if((self = [super init]) != nil) {

    } return self;
}

- (void)dealloc {
    [_view release];
    [_backgroundView release];
    [super dealloc];
}

- (void)loadFullView {
    // Add subviews to _backgroundView (or _view) here.
    UIButton *start = [UIButton buttonWithType:UIButtonTypeCustom];
    [start setTitle:@"Start:" forState:UIControlStateNormal];
    start.frame = CGRectMake(0, 0, 79, 33);
    [start addTarget:self action:@selector(timerStart) forControlEvents:UIControlEventTouchDown];
    [_view addSubview:start];
}

- (void)updateTimer:(NSTimer*)theTimer
{

    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:self.startDate];
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];


    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss.SSS"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];


    NSString *timeString = [dateFormatter stringFromDate:timerDate];
    self.stopWatchLabel.text = timeString;
}

- (void)timerStart
{
    self.startDate = [NSDate date];

    // Create the stop watch timer that fires every 10 ms
    self.stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                           target:self
                                                         selector:@selector(updateTimer)
                                                         userInfo:nil
                                                          repeats:YES];

    UIButton *stop = [UIButton buttonWithType:UIButtonTypeCustom];
    [stop setTitle:@"Stop:" forState:UIControlStateNormal];
    [stop addTarget:self action:@selector(timerStop) forControlEvents:UIControlEventTouchDown];
}

- (void)timerStop
{
    [self.stopWatchTimer invalidate];
    self.stopWatchTimer = nil;
    [self updateTimer];
}

- (void)loadPlaceholderView {
    // This should only be a placeholder - it should not connect to any servers or perform any intense
    // data loading operations.
    //
    // All widgets are 316 points wide. Image size calculations match those of the Stocks widget.
    _view = [[UIView alloc] initWithFrame:(CGRect){CGPointZero, {316.f, 33.f}}];
    _view.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    UIImage *bgImg = [UIImage imageWithContentsOfFile:@"/System/Library/WeeAppPlugins/StocksWeeApp.bundle/WeeAppBackground.png"];
    UIImage *stretchableBgImg = [bgImg stretchableImageWithLeftCapWidth:floorf(bgImg.size.width / 2.f) topCapHeight:floorf(bgImg.size.height / 2.f)];
    _backgroundView = [[UIImageView alloc] initWithImage:stretchableBgImg];
    _backgroundView.frame = CGRectInset(_view.bounds, 2.f, 0.f);
    _backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [_view addSubview:_backgroundView];
}

- (void)unloadView {
    [_view release];
    _view = nil;
    [_backgroundView release];
    _backgroundView = nil;
    // Destroy any additional subviews you added here. Don't waste memory :(.
}

- (float)viewHeight {
    return 71.f;
}

@end

Thanks in advance! It probably looks like a mess because it is..

Hbrewitt
  • 28
  • 5

1 Answers1

0

I think you are missing this line after you create the NSTimer:

[self.stopWatchTimer fire];

Don't know what you mean by "there is no 00.00.00." though.

EDIT: I'm sorry forget that, you don't need to call "fire" if you do scheduledTimerWithTimeInterval. But the selector must have the signature (in your case):

   - (void)updateTimer:(NSTimer*)theTimer {
...
}
self.stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                           target:self
                                                         selector:@selector(updateTimer:)
                                                         userInfo:nil
                                                          repeats:YES];

The timer sends itself in that message. Try that.

Odrakir
  • 4,254
  • 1
  • 20
  • 52
  • I've added that and still nothing. By, "There is no 00.00.00" I mean that nothing shows beside "Start:" I mean, no actual digits that would be counting up? Thanks for the reply. – Hbrewitt Feb 24 '13 at 09:06
  • I'm not entirely sure I understand how to implement the updated answer? Could you give me a walkthrough/better explanation? Sorry if that sounds rude, Im just quite new to this.. Thanks! – Hbrewitt Feb 24 '13 at 09:29
  • I updated my answer with the code you have to write. You just have to make "updateTimer" take an argument (the NSTimer) and also change the signature of that method in scheduleTimerWithTimeInterval to "@selector(updateTimer:)" – Odrakir Feb 24 '13 at 09:34
  • Okay, that makes a lot more sense. I've added the code and once I compile I get the following error. Line 85: 'NCTimerController' may not respond to '-updateTimer' (Messages without a matching method signature) – Hbrewitt Feb 24 '13 at 09:43
  • Check if you typed @selector(updateTimer:) with ":" at the end. – Odrakir Feb 24 '13 at 09:47
  • I did. Should I have? Compiled without it; still the same error. – Hbrewitt Feb 24 '13 at 09:50
  • I've updated the original code to match what I have right now; the only errors are running on line 85. Which is inside - (void)timerStop The error is with [self updateTimer]; – Hbrewitt Feb 24 '13 at 09:56
  • You shouldn't call updateTimer directly. But in case you do you are missing one argument, the timer. The method signature is "updateTimer:" not "updateTimer" – Odrakir Feb 24 '13 at 10:01
  • Okay, so. I've changed line 85 to updateTimer: and also changed @selector(updateTimer:) back.. I'm still getting errors still. I don't know what else needs to be done? All the errors are still within line 85. – Hbrewitt Feb 24 '13 at 10:21
  • As I said you shouldn't call [self updateTimer] inside timerStop. Besides I don't think that call makes sense at all. Remove it. If you still get errors maybe you should start another question, I think I helped you to fix the timer issue, didn't I? – Odrakir Feb 24 '13 at 11:20