2013-02-24 62 views
-1

我遇到了NSTimer問題,並在通知中心添加了秒錶;我對Obj C很陌生,並沒有使用Xcode。 Widget編譯並運行正常,但推「Start:」什麼都不做,並且沒有00.00.00。通知中心的NSTimer秒錶

#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 

在此先感謝! 它可能看起來像一個爛攤子,因爲它是..

回答

0

我認爲你缺少這一行,你創建的NSTimer後:

[self.stopWatchTimer fire]; 

不知道你的意思是「沒有00.00。 00.」雖然。

編輯:我很抱歉忘了,如果你使用scheduledTimerWithTimeInterval,你不需要調用「fire」。但選擇必須有簽名(你的情況):

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

定時器將自身發送該消息。試試看。

+0

我已經添加了,但仍然沒有。 By,「There is no 00.00.00」我的意思是沒有任何東西顯示在「開始」旁邊: 我的意思是,沒有實際的數字會計數? 感謝您的回覆。 – Hbrewitt 2013-02-24 09:06:14

+0

我更新了我的答案。 – Odrakir 2013-02-24 09:13:57

+0

我不完全確定我理解如何實現更新的答案? 你能給我一個演練/更好的解釋嗎? 對不起,如果這聽起來粗魯,我只是相當新的這個.. 謝謝! – Hbrewitt 2013-02-24 09:29:11