【问题标题】:NSTimer Stopwatch in Notification Centre通知中心的 NSTimer 秒表
【发布时间】:2013-02-24 08:33:08
【问题描述】:

我在使用 NSTimer 并将秒表添加到通知中心时遇到问题;我对 Obj C 非常陌生,并且没有使用 Xcode。小部件编译并运行良好,但按下“开始:”没有任何作用,并且没有 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

提前致谢! 它可能看起来像一团糟,因为它是..

【问题讨论】:

    标签: ios objective-c nstimer stopwatch notificationcenter


    【解决方案1】:

    我认为您在创建 NSTimer 后缺少这一行:

    [self.stopWatchTimer fire];
    

    不知道“没有 00.00.00”是什么意思。不过。

    编辑:很抱歉忘记了这一点,如果您执行 scheduleTimerWithTimeInterval,则无需调用“fire”。但是选择器必须有签名(在你的情况下):

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

    计时器在该消息中发送自己。试试看。

    【讨论】:

    • 我已经添加了,但仍然没有。通过,“没有 00.00.00”我的意思是“开始:”旁边没有任何显示,我的意思是,没有实际的数字可以计数?感谢您的回复。
    • 我不完全确定我了解如何实施更新的答案?你能给我一个演练/更好的解释吗?对不起,如果这听起来很粗鲁,我对此很陌生..谢谢!
    • 我用您必须编写的代码更新了我的答案。您只需让“updateTimer”接受一个参数(NSTimer),并将 scheduleTimerWithTimeInterval 中该方法的签名更改为“@selector(updateTimer:)”
    • 好吧,这更有意义。我添加了代码,编译后出现以下错误。第 85 行:“NCTimerController”可能不会响应“-updateTimer”(没有匹配方法签名的消息)
    • 检查你是否在最后输入了@selector(updateTimer:) 并带有“:”。
    猜你喜欢
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 2014-10-11
    • 1970-01-01
    • 2016-09-26
    • 2014-07-15
    • 1970-01-01
    • 2021-12-13
    相关资源
    最近更新 更多