ios中 pickerView的用法
Posted zzqqrr
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios中 pickerView的用法相关的知识,希望对你有一定的参考价值。
今天是一个特殊的日子(Mac pro 敲的 爽。。。 昨天到的)
// // QRViewController.m// #import "QRViewController.h" @interface QRViewController ()<UIPickerViewDataSource,UIPickerViewDelegate> @property (weak, nonatomic) IBOutlet UILabel *fruitLable;//水果 @property (weak, nonatomic) IBOutlet UILabel *mainLabel;//主菜 @property (weak, nonatomic) IBOutlet UILabel *drinkLabel;//饮料 @property (nonatomic,strong) NSArray *foods; @property (weak, nonatomic) IBOutlet UIPickerView *pickerView; - (IBAction)ranDom; @end @implementation QRViewController - (void)viewDidLoad { [super viewDidLoad]; for (int i=0; i<self.foods.count; i++) { [self pickerView:nil didSelectRow:0 inComponent:i]; } //[self pickerView:nil didSelectRow:0 inComponent:0]; //[self pickerView:nil didSelectRow:0 inComponent:1]; //[self pickerView:nil didSelectRow:0 inComponent:2]; } /** *懒加载数据 */ - (NSArray *)foods { if(_foods==nil){ NSString *path=[[NSBundle mainBundle] pathForResource:@"foods" ofType:@"plist"]; NSArray *arraylist=[NSArray arrayWithContentsOfFile:path]; _foods=arraylist; } return _foods; } #pragma mark -数据源方法 /** *一共多少列 */ - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return self.foods.count; } /** * 某一列显示多少行 */ - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { NSArray *subfoods=self.foods[component]; return subfoods.count; } #pragma mark - 代理方法 /** *某一列中的某一行显示的数据 */ - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return self.foods[component][row]; } /** *选中某一行一列 */ - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { if(component==0){//水果 self.fruitLable.text=self.foods[component][row]; }else if(component==1){//主菜 self.mainLabel.text=self.foods[component][row]; }else if(component==2){//饮料 self.drinkLabel.text=self.foods[component][row]; } } /** *随机生成一个 */ - (IBAction)ranDom { for (int i=0; i<self.foods.count; i++) { //每一行的总长度 int count=[self.foods[i] count]; //生成一个随机数 int row=arc4random()%count; //设置pickerView选中的列的行 [self.pickerView selectRow:row inComponent:i animated:YES]; //设置Label的文字 [self pickerView:nil didSelectRow:row inComponent:i]; } } @end
以上是关于ios中 pickerView的用法的主要内容,如果未能解决你的问题,请参考以下文章
如何在 ios 中点击 UITextView 打开 Pickerview(dropdown)?
ios 中pickerView城市选择和UIDatePicker生日选择
带有pickerview的UItextfield作为第一响应者ios