プロジェクト

全般

プロフィール

操作

ハンズオン資料

BMIアプリのコード

  • リスト02-01
//
//  ViewController.m
//
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *weightText;
@property (weak, nonatomic) IBOutlet UITextField *heightText;
@property (weak, nonatomic) IBOutlet UILabel     *resultLabel;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)bmiButtonPushed:(id)sender 
{
    float h = _heightText.text.floatValue / 100;
    float w = _weightText.text.floatValue;

    float bmi = w / (h * h);
    NSLog(@"bmi = %f", bmi);

    if (bmi >= 25) {
        _resultLabel.text = @"太りすぎ";
    } else if (bmi < 18.5) {
        _resultLabel.text = @"やせすぎ";
    } else {
        _resultLabel.text = @"標準";
    }
}
@end
  • リスト02-02
    if (bmi >= 25) {
        _resultLabel.text = @"太りすぎ";
        _resultLabel.backgroundColor = [UIColor redColor]; 
    } else if (bmi < 18.5) {
        _resultLabel.text = @"やせすぎ";
        _resultLabel.backgroundColor = [UIColor yellowColor];
    } else {
        _resultLabel.text = @"標準";
        _resultLabel.backgroundColor = [UIColor greenColor]; 
    }
  • リスト02-03
#import <AVFoundation/AVFoundation.h>
  • リスト02-04
@property (strong, nonatomic) AVAudioPlayer      *audioPlayer;
  • リスト02-05
- (void) sound:(NSString *)file 
{
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
        pathForResource:file ofType:@"aif"]];
    _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [_audioPlayer play];
}
  • リスト02-06
    if (bmi >= 25) {
        _resultLabel.text = @"太りすぎ";
        _resultLabel.backgroundColor = [UIColor redColor];
        [self sound:@"fat"];
    } else if (bmi < 18.5) {
        _resultLabel.text = @"やせすぎ";
        _resultLabel.backgroundColor = [UIColor yellowColor];
        [self sound:@"skinny"];
    } else {
        _resultLabel.text = @"標準";
        _resultLabel.backgroundColor = [UIColor greenColor];
        [self sound:@"normal"]; 
    }

サウンドファイル

Yuumi Yoshida さんが8年以上前に更新 · 3件の履歴