UISegmentedControlは数個の選択肢から排他的に選択する場合に有効だそうです。
参考にさせて頂いたサイト。
Objective-C Segmented(セグメント) – iscene ページ!
ゲームのLevel、難易度に使用しました。
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 |
#import "ViewController.h" @interface ViewController ()//<UIAlertViewDelegate> { int level;//Levelの設定 } - (IBAction)levelChange:(UISegmentedControl *)sender; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //レベルの初期値 level = 10; } #pragma mark - レベルの設定 - (IBAction)levelChange:(UISegmentedControl *)sender { switch (sender.selectedSegmentIndex) { case 0: level = 10; _recordHyouji.text = [NSString stringWithFormat:@"%04d",recordCount];//それぞれ表示 _timeHyouji.text = [NSString stringWithFormat:@"%05.2f",10.00]; break; case 1: level = 30; _recordHyouji.text = [NSString stringWithFormat:@"%04d",recordCount30];// _timeHyouji.text = [NSString stringWithFormat:@"%05.2f",30.00]; break; case 2: level = 60; _recordHyouji.text = [NSString stringWithFormat:@"%04d",recordCount60];// _timeHyouji.text = [NSString stringWithFormat:@"%05.2f",60.00]; break; } } |
ボタンを選択すると、選択した番号を持ってaction接続したメソッドへ飛びます。
そこでswitch文でそれぞれにあった動作をさせます。