IOSbookSeminar1 » 履歴 » バージョン 2
Yuumi Yoshida, 2013-04-09 08:54
1 | 1 | Yuumi Yoshida | h1. ハンズオン資料 |
---|---|---|---|
2 | |||
3 | 2 | Yuumi Yoshida | h3. BMIアプリのコード |
4 | 1 | Yuumi Yoshida | |
5 | 2 | Yuumi Yoshida | * リスト02-01 |
6 | |||
7 | 1 | Yuumi Yoshida | <pre> |
8 | 2 | Yuumi Yoshida | // |
9 | // ViewController.m |
||
10 | // |
||
11 | // |
||
12 | |||
13 | #import "ViewController.h" |
||
14 | |||
15 | @interface ViewController () |
||
16 | @property (weak, nonatomic) IBOutlet UITextField *weightText; |
||
17 | @property (weak, nonatomic) IBOutlet UITextField *heightText; |
||
18 | @property (weak, nonatomic) IBOutlet UILabel *resultLabel; |
||
19 | @end |
||
20 | |||
21 | @implementation ViewController |
||
22 | |||
23 | - (void)viewDidLoad |
||
24 | { |
||
25 | [super viewDidLoad]; |
||
26 | } |
||
27 | |||
28 | - (void)didReceiveMemoryWarning |
||
29 | { |
||
30 | [super didReceiveMemoryWarning]; |
||
31 | } |
||
32 | |||
33 | - (IBAction)bmiButtonPushed:(id)sender |
||
34 | { |
||
35 | float h = _heightText.text.floatValue / 100; |
||
36 | float w = _weightText.text.floatValue; |
||
37 | |||
38 | float bmi = w / (h * h); |
||
39 | NSLog(@"bmi = %f", bmi); |
||
40 | |||
41 | if (bmi >= 25) { |
||
42 | _resultLabel.text = @"太りすぎ"; |
||
43 | } else if (bmi < 18.5) { |
||
44 | _resultLabel.text = @"やせすぎ"; |
||
45 | } else { |
||
46 | _resultLabel.text = @"標準"; |
||
47 | } |
||
48 | } |
||
49 | @end |
||
50 | 1 | Yuumi Yoshida | </pre> |
51 | |||
52 | 2 | Yuumi Yoshida | * リスト02-02 |
53 | |||
54 | <pre> |
||
55 | if (bmi >= 25) { |
||
56 | _resultLabel.text = @"太りすぎ"; |
||
57 | _resultLabel.backgroundColor = [UIColor redColor]; |
||
58 | } else if (bmi < 18.5) { |
||
59 | _resultLabel.text = @"やせすぎ"; |
||
60 | _resultLabel.backgroundColor = [UIColor yellowColor]; |
||
61 | } else { |
||
62 | _resultLabel.text = @"標準"; |
||
63 | _resultLabel.backgroundColor = [UIColor greenColor]; |
||
64 | } |
||
65 | </pre> |
||
66 | |||
67 | * リスト02-03 |
||
68 | |||
69 | <pre> |
||
70 | #import <AVFoundation/AVFoundation.h> |
||
71 | </pre> |
||
72 | |||
73 | |||
74 | * リスト02-04 |
||
75 | |||
76 | <pre> |
||
77 | @property (strong, nonatomic) AVAudioPlayer *audioPlayer; |
||
78 | </pre> |
||
79 | |||
80 | |||
81 | * リスト02-05 |
||
82 | |||
83 | <pre> |
||
84 | - (void) sound:(NSString *)file |
||
85 | { |
||
86 | NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] |
||
87 | pathForResource:file ofType:@"aif"]]; |
||
88 | _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; |
||
89 | [_audioPlayer play]; |
||
90 | } |
||
91 | </pre> |
||
92 | |||
93 | |||
94 | * リスト02-06 |
||
95 | |||
96 | <pre> |
||
97 | if (bmi >= 25) { |
||
98 | _resultLabel.text = @"太りすぎ"; |
||
99 | _resultLabel.backgroundColor = [UIColor redColor]; |
||
100 | [self sound:@"fat"]; |
||
101 | } else if (bmi < 18.5) { |
||
102 | _resultLabel.text = @"やせすぎ"; |
||
103 | _resultLabel.backgroundColor = [UIColor yellowColor]; |
||
104 | [self sound:@"skinny"]; |
||
105 | } else { |
||
106 | _resultLabel.text = @"標準"; |
||
107 | _resultLabel.backgroundColor = [UIColor greenColor]; |
||
108 | [self sound:@"normal"]; |
||
109 | } |
||
110 | </pre> |
||
111 | |||
112 | h3. サウンドファイル |