#import "ViewController.h" @interface ViewController () <UIPickerViewDelegate,UIPickerViewDataSource> { NSArray *teaList; } @property (weak, nonatomic) IBOutlet UILabel *teaLabel; @property (weak, nonatomic) IBOutlet UIPickerView *myPicker; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.myPicker.dataSource = self; self.myPicker.delegate = self; [self setTapGesture]; //データを用意 teaList = @[@"ダージリン",@"アールグレイ",@"アッサム",@"オレンジペコ"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)setTapGesture{ // シングルタップ UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(teaLabel:)]; // デリゲートをセット tapGesture.delegate = self; // view に追加 [self.view addGestureRecognizer:tapGesture]; } //-- ピッカーに列数を渡す -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } //-- ピッカーに行数を返す -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return teaList.count; } //-- ピッカーに文字を返す -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return teaList[row]; } //-- ピッカーで選択されたときに行う処理 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { NSLog(@"選択=%@", teaList[row]); } //-- LabelタップでPickerを非表示 - (void)teaLabel:(UITapGestureRecognizer *)sender{ _myPicker.hidden = NO; } @end
上記のコードを記述しteaLabelをタップするとPickerViewを表示しteaListの中身を
NSLogで表示しています。
Pickerで表示しているteaListの値をteaLabelに反映させるには、どのように記述したらよろしいでしょうか?
ネット等で調べましたが、なかなか参考となるものがなかったためご質問させていただきました。
ご教示いただけましたら、幸いです。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/03 04:04