標籤:plist oftype file 文字 nbu picker type 今天 blog
今天是一個特殊的日子(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的用法