入力した数字の計算と表示。

めちゃくちゃ今更なんですが、計算アプリ作ってて入力されたものを計算と表示する手順がわからんかったので備忘録。

参考にさせて頂いたサイト
Objective-C 簡易計算機 – iscene ページ!

タイムラプスの計算アプリ作ってて、インターバルとFPSから速さ(倍速)を計算する部分から抜粋。

iOS Simulator Screen Shot 2015.10.10 14.21.35

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *intervalSecond;
@property (weak, nonatomic) IBOutlet UITextField *fpsCount;
@property (weak, nonatomic) IBOutlet UILabel *speedCount;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self speedKeisan];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) speedKeisan
{
    // 要素値の取得 NSString:文字列の値 テキストから文字列に代入
    NSString *intStr = _intervalSecond.text;
    NSString *fpsStr = _fpsCount.text;
    
    // 要素値のintValue 文字→数字 型変換 int:数値を扱う値
    int interval = [intStr intValue];
    int fpsSuu = [fpsStr intValue];
    
    //数値speedを作成して計算
    int speed = interval * fpsSuu;
    
    //速さのテキスト変数を作って数字をテキストに変換
    NSString *speedStr = [NSString stringWithFormat:@"%d倍速",speed];
    
    //速さを表示
    self.speedCount.text = speedStr;
}
@end

変数の変換やらなんやらがめんどくさいね。

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です