こんにちは。Objective-CでiOSアプリの勉強をしています。
インターフェースビルダーは使っていません。
簡単なストップウォッチをつくろうとしているのですが、わからない点があります。
lang
1//ViewController.m 2@implementation ViewController 3- (void)viewDidLoad { 4 [super viewDidLoad]; 5 6 //スタートボタン 7 UIButton *startButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 8 [startButton addTarget:self action:@selector(tobFire) forControlEvents:UIControlEventTouchUpInside]; 9 [self.view addSubview:startButton]; 10 11 //ストップボタン 12 UIButton *stopButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 13 [stopButton addTarget:self action:@selector(tobStop) forControlEvents:UIControlEventTouchUpInside]; 14 [self.view addSubview:stopButton]; 15 16 //ここでラベル生成 17 UILabel *timeLabel = [[UILabel alloc]init]; 18 timeLabel.text = @"0"; 19 timeLabel.frame = CGRectMake(150, 200, 100, 150); 20 [self.view addSubview:timeLabel]; 21} 22 23//1秒ごとに呼び出されます。 24- (void)timeCount:(NSTimer*)timer { 25 self->time++; 26 NSLog(@"%d",self->time); 27 //ここでUILabelのtextに書き込みたい 28 29} 30 31//スタートボタンを押した時 32//tobはNSTimerで@propertyで宣言しています。 33- (void)tobFire{ 34 self.tob = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeCount:) userInfo:nil repeats:YES]; 35 if(self.tob.isValid == 0){ 36 [self.tob fire];} 37} 38//ストップを押した時 39- (void)tobStop{ 40 if(self.tob.isValid == 1){ 41 [self.tob invalidate];} 42} 43@end 44
viewDidLoadの中でUILabelを生成し、timeCountの中で label.text = self->timeとやりたいのですが、(timeはインスタンス変数です。)
スコープ?というのでしょうか、このような場合にはどのようにしたらいいのでしょうか。
timeCountの中でlabel.textだと認識できないとエラーが出できます。
UILabelを@propertyで宣言しても上手くいかないのです。
また、もっとコードを組んだり、読んだりしたいのですが、
おすすめの書籍やサンプル等があれば教えていただけないでしょうか。
絶対的にプログラムの経験が少ないので、例題が豊富なものだと嬉しいです。
よろしくお願い致します。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。