プロジェクト

全般

プロフィール

操作

IOSbookSeminar1 » 履歴 » リビジョン 2

« 前 | リビジョン 2/3 (差分) | 次 »
Yuumi Yoshida, 2013-04-09 08:54


h1. ハンズオン資料

h3. 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

  • リスト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"];
}

h3. サウンドファイル

Yuumi Yoshida さんが約11年前に更新 · 2件の履歴