1、UIDatePicker 是一个可以用来选择或者设置日期的控件,不过它是像转轮一样的控件,而且是苹果专门为日历做好的控件。
2、由于没有xCode所以我就简单写几行代码来向大家介绍一下UIDatePicker控件的基本创建和使用,望见谅。
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,460)];
self.view = myView;
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,0,320,300)];
datePicker.date = [NSDate date]; // 设置日历控件显示日期为当前日期
datePicker.tag = 100;
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[myView addSubview:datePicker];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(100,300,120,50);
[btn setTitle:@“时间”forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnEventMathod:)forControlEvents:UIControlEventTouchDown];
[myView addSubview:btn];
[myView release];
-(void)btnEventMathod:(UIButton *)sender
{
UIDatePicker *picker = (UIDatePicker *)[self.view viewWithTag:100];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @“yyyy-MM-dd HH:mm:ss”;
NSLog(@“%@”, [formatter stringFromDate:picker.date]);
}
3、UIPickerView的使用
UIPickerView 为用户提供了选择器功能,使用户以更好的体验方式实现数据的选择。
先在 .h 文件里将创建一个UIPickerView对象、一个UILabel对象以及一个NSArray对象,并将它们定义成属性。
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,460)];
self.view = myView;
[myView release];
self.content = [[NSArray alloc] initWithObjects:@“水瓶座”,@“双鱼座”,@“白羊座”,@“金牛座”,@“双子座”,@“巨蟹座”,@“狮子座”,@“处女座”,@“天秤座”,@“天蝎座”,@“射手座”,@“山羊座”,nil]; // 初始化数组,这些数据将被显示在piker中。
self.pickerview = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 150.0, 320.0, 216.0)]; // 设置选择器
self.pickerview.delegate = self; // 设置代理
self.pickerview.showsSelectionIndicator = YES;
[self.view addSubview:self.pickerview];
self.contentview = [[UILabel alloc] initWithFrame:CGRectMake(80.0, 80.0, 100.0, 40.0)];
self.contentview.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.contentview];
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
return 1;
} // 返回显示的列数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.content count];
} // 返回当前显示的列数
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.content objectAtIndex:row];
} // 设置当前行的内容,如果行没有显示则自动释放
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSString *result = nil;
result = [self.content objectAtIndex:row];
self.contentview.text = result;
[result release];
}
PS:(没有xCode好悲催、、555~~)