1. 程式人生 > >ios中 pickerView的用法

ios中 pickerView的用法

source itl -a tom imp oftype -1 picker image

今天是一個特殊的日子(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的用法