UIDatePicker
属性: Mode:Date and Time Locale: Interval: 最大值,最小值 NSDate *date=[self.dataPicker date]; NSDateFormatter *formatter=[[NSDateFormatter alloc]init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *date2=[formatter stringFromDate:date];
UIPickerView
代理方法: delegate,dataSource NSArray *data=[[NSArray alloc]initWithObjects:@"中国",@"日本",@"美国",@"法国",@"英国",@"意大利", nil]; #pragma mark --UIDataPicker DataSource Method-- - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1;//返回组件的个数,即列数 } -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return [data count];//行数 } -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [data objectAtIndex:row];//每一行对应的标题 } NSInteger row=[singlePick selectedRowInComponent:0]; NSString *selected=[data objectAtIndex:row]; NSString *title=[NSString stringWithFormat:@"当前选中的国家是:%@",selected]; UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:title message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alertView show];
多组件选择器:
#import "ThridViewController.h" @interface ThridViewController () @property (weak, nonatomic) IBOutlet UIPickerView *picker; @property (nonatomic,strong) NSDictionary *dic; @property (nonatomic,strong) NSArray *provinces; @property (nonatomic,strong) NSArray *city; @end @implementation ThridViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. NSString *plistCity=[[NSBundle mainBundle]pathForResource:@"cities" ofType:@"plist"]; NSDictionary *dic=[NSDictionary dictionaryWithContentsOfFile:plistCity]; self.dic=dic; NSArray *arrayKey=[dic allKeys]; self.provinces=[arrayKey sortedArrayUsingSelector:@selector(compare:)]; NSString *provicesName=[self.provinces objectAtIndex:0]; self.city= [dic objectForKey:provicesName]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ #pragma mark //numberOfComponentsInPickerView -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 2; } -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { if (component==0) { return [self.provinces count]; } else { return [self.city count]; } } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if (component==0) { return [self.provinces objectAtIndex:row]; } return [self.city objectAtIndex:row]; } - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{ if (component==0) { return 200; } return 100; } #pragma mark --delegate-- - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if (component==0) { NSString *keyName=[self.provinces objectAtIndex:row]; NSArray *array=[self.dic objectForKey:keyName]; self.city=array; [self.picker selectRow:0 inComponent:1 animated:YES]; [self.picker reloadComponent:1]; } } - (IBAction)btnOK:(id)sender { NSInteger row1= [self.picker selectedRowInComponent:0]; NSString *proName=[self.provinces objectAtIndex:row1]; NSInteger row2=[self.picker selectedRowInComponent:1]; NSString *cityName=[self.city objectAtIndex:row2]; NSString *title=[NSString stringWithFormat:@"您选择的省份:%@,城市:%@",proName,cityName]; UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:title message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alertView show]; } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSString *content= alertView.title; NSLog(@"%ld....%@",(long)buttonIndex,content); }
时间: 2024-10-18 17:03:46