在 .m 文件上执行此操作
UIViewController *popoverContent = [[UIViewController alloc]init];
UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
datepiker= [[UIDatePicker alloc]init];
[datepiker setFrame:CGRectMake(0, 0, 320, 216)];
datepiker.datePickerMode=UIDatePickerModeDateAndTime;
datepiker.hidden=NO;
datepiker.minimumDate=[NSDate date];
[self.view addSubview:datepiker];
[datepiker release];
// [datepiker addTarget:self action:@selector(changedDate:) forControlEvents:UIControlEventValueChanged];
btn_add=[[UIButton alloc]initWithFrame:CGRectMake(115, 250, 100, 30)];
[btn_add setTitle:@"Add" forState:UIControlStateNormal];
[btn_add setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20]];
[btn_add setBackgroundColor:[UIColor redColor]];
[btn_add addTarget:self action:@selector(AddDate:) forControlEvents:UIControlEventTouchUpInside];
[popoverView addSubview:btn_add];
btn_cancel=[[UIButton alloc]initWithFrame:CGRectMake(115, 300, 100, 30)];
[btn_cancel setTitle:@"Cancel" forState:UIControlStateNormal];
[btn_cancel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20]];
[btn_cancel setBackgroundColor:[UIColor redColor]];
[btn_cancel addTarget:self action:@selector(CancelDate:) forControlEvents:UIControlEventTouchUpInside];
[popoverView addSubview:btn_cancel];
[popoverView addSubview:datepiker];
[popoverView addSubview:btn_add];
popoverContent.view = popoverView;
popoverContent.contentSizeForViewInPopover = CGSizeMake(320,350);
self.popoverController = [[UIPopoverController alloc]
initWithContentViewController:popoverContent];
[self.popoverController presentPopoverFromRect:CGRectMake(400,-150, 320,220)
inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
[popoverView release];
[popoverContent release];
}
-(void)AddDate:(id)sender
{
}
-(void)CancelDate:(id)sender
{
[popoverController dismissPopoverAnimated:YES];
}
在 .h 文件上执行此操作并添加 UIPickerViewDelegate,UIPopoverControllerDelegate
{
UIPopoverController *popoverController;
UIDatePicker *datepiker;
UIPickerView *picker;
UIButton *btn_add;
UIButton *btn_cancel;
}
@property(nonatomic,retain)IBOutlet UIDatePicker *datepiker;
@property(nonatomic,retain)IBOutlet UIPickerView *picker;
@property(nonatomic,retain)UIPopoverController *popoverController;
@property(nonatomic,retain)UIButton *btn_add;
@property(nonatomic,retain)UIButton *btn_cancel;
-(void)AddDate:(id)sender;
-(void)CancelDate:(id)sender;
这对我有用。恩乔伊