Xcode 4.2 で NSTimer を作成しましたが、動作しますが、この 1 つの問題が発生します。
これがシミュレーターでの私のプロジェクトです
開始を押すと開始し、停止を押すと停止し、停止するとリセットされますが、開始すると何も起こらず、リセットを押すと、基本的に停止する必要があります。方法とこれまたはコードのコピーをどこにでも追加する必要がありますか。
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController {
IBOutlet UILabel *time;
NSTimer *myticker;
//declare baseDate
NSDate* baseDate;
}
-(IBAction)stop;
-(IBAction)reset;
@end
ここに私の実装があります
#import "FirstViewController.h"
@implementation FirstViewController
@synthesize baseDate;
-(IBAction)start {
[myticker invalidate];
self.baseDate = [NSDate date];
myticker = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(showActivity) userInfo:nil repeats:YES];
}
-(IBAction)stop;{
[myticker invalidate];
myticker = nil;
}
-(IBAction)reset {
self.baseDate = [NSDate date];
time.text = @"00:00:0";
}
-(void)showActivity {
NSTimeInterval interval = [baseDate timeIntervalSinceNow];
double intpart;
double fractional = modf(interval, &intpart);
NSUInteger hundredth = ABS((int)(fractional*10));
NSUInteger seconds = ABS((int)interval);
NSUInteger minutes = seconds/60;
time.text = [NSString stringWithFormat:@"%02d:%02d:%01d", minutes%60, seconds%60, hundredth];
}
ほんとうにありがとう。ありがとう。