質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Q&A

4回答

1917閲覧

Objective-CによるiOSアプリ開発でわからない点があります。

退会済みユーザー

退会済みユーザー

総合スコア0

Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

0グッド

0クリップ

投稿2015/07/09 01:35

こんにちは。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で宣言しても上手くいかないのです。

また、もっとコードを組んだり、読んだりしたいのですが、
おすすめの書籍やサンプル等があれば教えていただけないでしょうか。
絶対的にプログラムの経験が少ないので、例題が豊富なものだと嬉しいです。

よろしくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答4

0

lang

1// ViewController.h 2 3@interface ViewController: UIViewController 4 5// ヘッダーファイルに変数を宣言してもいいですし... 6@property (nonatomic, strong) UILabel* label; 7@property (nonatomic) NSInteger time; 8 9@end 10 11//ViewController.m 12 13#import "ViewController.h" 14 15// 実装ファイルに変数を宣言するのも大丈夫です。 16@interface ViewController() 17@property (nonatomic, strong) UILabel *label; 18@property (nonatomic) NSInteger time; 19@end 20 21@implementation ViewController 22 23- (void)viewDidLoad { 24 [super viewDidLoad]; 25 26 //スタートボタン 27 UIButton *startButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 28 [startButton addTarget:self action:@selector(tobFire) forControlEvents:UIControlEventTouchUpInside]; 29 [self.view addSubview:startButton]; 30 31 //ストップボタン 32 UIButton *stopButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 33 [stopButton addTarget:self action:@selector(tobStop) forControlEvents:UIControlEventTouchUpInside]; 34 [self.view addSubview:stopButton]; 35 36 //ここでラベル生成 37 UILabel *timeLabel = [[UILabel alloc]init]; 38 timeLabel.text = @"0"; 39 timeLabel.frame = CGRectMake(150, 200, 100, 150); 40 self.label = timeLabel; 41 [self.view addSubview:timeLabel]; 42} 43 44//1秒ごとに呼び出されます。 45- (void)timeCount:(NSTimer*)timer { 46 self.time++; 47 NSLog(@"%d",self.time); 48 //ここでUILabelのtextに書き込みたい 49 // ↓以下のようにUILabelのtextプロパティにNSStringを渡せば、描画されます。 50 self.label.text = [NSString stringWithFormat: @"%d", (int)self.time]; 51} 52 53//スタートボタンを押した時 54//tobはNSTimerで@propertyで宣言しています。 55- (void)tobFire{ 56 self.tob = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeCount:) userInfo:nil repeats:YES]; 57 if(self.tob.isValid == 0){ 58 [self.tob fire]; 59 } 60} 61//ストップを押した時 62- (void)tobStop{ 63 if(self.tob.isValid == 1){ 64 [self.tob invalidate]; 65 } 66} 67@end

補足ですが、ヘッダーファイルにクラスが持つすべてのpropertyを書いてしまうのは、よくないです。
なので、そのクラスでしか使わないpropertyは実装ファイル内で宣言するほうが望ましいと思います。

lang

1// ViewController.m 2@interface ViewController() 3@property (nonatomic, strong) UILabel* label; 4@property (nonatomic) NSInteger time; 5@end 6 7@implementation ViewController 8 9// 諸々実装 10 11@end

他のクラスに知られたくないこと(知る必要がないこと)は実装ファイルに書いてしまいましょう。

投稿2015/07/11 16:54

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ViewController.mの最上部に

lang

1@interface ViewController() 2@property (strong, nonatomic) UILabel *label; 3@end

これでOK。
ほかのクラスから参照する必要がなければ、この形がいいかと思います。

僕はiOS始めた頃、サルできにお世話になってましたね。
http://sarudeki.jp

投稿2015/07/11 16:23

編集2015/07/11 16:28
masashimizuno

総合スコア42

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

labelをviewDidLoadのローカル変数ではなく、ViewControllerのインスタンス変数にしてください。

投稿2015/07/09 10:23

Stripe

総合スコア2183

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

.mファイル内に@property宣言し、self.label.textで出来るかと思います。

.mじゃありませんでした。.hでした。

投稿2015/07/09 02:45

編集2015/07/09 23:29
orange0190

総合スコア1698

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問