入力した数字の計算と表示。
めちゃくちゃ今更なんですが、計算アプリ作ってて入力されたものを計算と表示する手順がわからんかったので備忘録。 参考にさせて頂いたサイト Objective-C 簡易計算機 – iscene ページ! タイムラプスの計算アプリ作ってて、インターバルとFPSから速さ(倍速)を計算する部分から抜粋。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#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 |
変数の変換やらなんやらがめ… 続きを読む入力した数字の計算と表示。