1 前言
UIPickerView(選擇器)也是出鏡率相當高的控件,類似與HTML中的Select,今天我們就一起來學習一下這個控件。
2 UIPickerView簡介
關鍵代碼:
.h文件: #import <UIKit/UIKit.h> //需要實現UIPickerViewDataSource,UIPickerViewDelegate協議 @interface ZYViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate> @property(nonatomic,strong) UIPickerView *myPicker; @end #import <UIKit/UIKit.h> //需要實現UIPickerViewDataSource,UIPickerViewDelegate協議 @interface ZYViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate> @property(nonatomic,strong) UIPickerView *myPicker; @end.m文件: @implementation ZYViewController @synthesize myPicker; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; self.myPicker = [[UIPickerView alloc] init]; self.myPicker.dataSource = self; self.myPicker.center = self.view.center; self.myPicker.delegate = self; //設置選擇器的水平的陰影效果 self.myPicker.showsSelectionIndicator = YES; [self.view addSubview:self.myPicker]; } // returns the number of 'columns' to display. //為選擇器添加組建,如果需要多個組建,可以修改該方法體 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ NSInteger result = 0; if([pickerView isEqual:self.myPicker]){ result = 1; } return result; } // returns the # of rows in each component.. //定義一個組件中有多少個選項,如果想為想象添加值,需要修改該方法體 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ NSInteger result = 0; if ([pickerView isEqual:self.myPicker]) { result = 10; } return result; } //UIPickerViewDelegate //設置UIPickerView的每個組件的標題 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ NSString *result = nil; if ([pickerView isEqual:self.myPicker]) { result = [NSString stringWithFormat:@"Row %ld",(long)row+1]; } return result; } //選擇時候觸發的事件 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { NSLog(@"result: You selected Row%ld",(long)row+1); } @implementation ZYViewController @synthesize myPicker; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; self.myPicker = [[UIPickerView alloc] init]; self.myPicker.dataSource = self; self.myPicker.center = self.view.center; self.myPicker.delegate = self; //設置選擇器的水平的陰影效果 self.myPicker.showsSelectionIndicator = YES; [self.view addSubview:self.myPicker]; } // returns the number of 'columns' to display. //為選擇器添加組建,如果需要多個組建,可以修改該方法體 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ NSInteger result = 0; if([pickerView isEqual:self.myPicker]){ result = 1; } return result; } // returns the # of rows in each component.. //定義一個組件中有多少個選項,如果想為想象添加值,需要修改該方法體 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ NSInteger result = 0; if ([pickerView isEqual:self.myPicker]) { result = 10; } return result; } //UIPickerViewDelegate //設置UIPickerView的每個組件的標題 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ NSString *result = nil; if ([pickerView isEqual:self.myPicker]) { result = [NSString stringWithFormat:@"Row %ld",(long)row+1]; } return result; } //選擇時候觸發的事件 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { NSLog(@"result: You selected Row%ld",(long)row+1); }
運行結果:
當選擇某個選項時候控制台運行結果:
2015-07-26 15:27:21.163 UIPickerViewTest[649:c07] result: You selected Row1
2015-07-26 15:31:49.433 UIPickerViewTest[649:c07] result: You selected Row2
2015-07-26 15:31:51.327 UIPickerViewTest[649:c07] result: You selected Row4