在storyboard中拖入Picker View控件
按住Ctrl鍵拖入代碼
在頭文件中加入data source和delegate協議,定義Picker值的Array
@interface Picking_Values_with_UIPickerViewViewController
: UIViewController
{
NSArray *PickerData;
NSArray1 *PickerData1;
}
在viewDidLoad方法中添加 self.myPicker.dataSource = self;
self.myPicker.delegate = self;
設置Picker中的值的Array
NSArray *dataArray = [[NSArray alloc]initWithObjects:@"你", @"我",@"他",nil];
NSArray *dataArray1 = [[NSArray alloc]initWithObjects:@"you", @"me",@"he",nil];
設置Picker中的值
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
NSInteger result = 0;
if ([pickerView isEqual:self.myPicker]) {
result =2; //2列
}
return result;
}
獲取選擇的值:
-(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]; } if (component == 0) { return [pickerData objectAtIndex:row]; } else { return [pickerData1 objectAtIndex:row]; }