2013-02-24 3 views
-1

NSTimer에 문제가 있고 알림 센터에 스톱워치를 추가하고 있습니다. Obj C에 익숙하지 않고 Xcode를 사용하지 않습니다. 위젯은 컴파일되고 정상적으로 실행되지만 "시작 :"을 누르면 아무것도 수행되지 않고 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 

미리 감사드립니다.

[self.stopWatchTimer fire]; 

는 "아무 00.00 없습니다에 의해 당신이 무슨 뜻인지 몰라 : 당신은 NSTimer를 작성 후이기 때문에 그것은 아마 엉망처럼 보인다 ..

답변

0

난 당신이 줄을 누락 생각합니다. 00 " 그래도.

편집 : 미안합니다. scheduledTimerWithTimeInterval을 수행하면 "불"을 호출 할 필요가 없습니다. 하지만 선택자는 서명을해야합니다 (귀하의 경우) :

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

타이머는 그 메시지에서 자신을 보냅니다. 시도해 봐.

+0

나는 아직 아무것도 추가하지 않았습니다. "00.00.00"이 없습니다. "시작 :"옆에 아무 것도 표시되지 않는다는 것을 의미합니다. 실제로 숫자가 올라가는 것은 아닙니다. 답장을 보내 주셔서 감사합니다. – Hbrewitt

+0

내 대답이 업데이트되었습니다. – Odrakir

+0

업데이트 된 답변을 구현하는 방법을 잘 모르겠습니다. 연습/더 나은 설명을 주시겠습니까? 죄송합니다. 무례한 소리가 들리면, 이것에 대해 아주 새로 ... 고마워! – Hbrewitt