我正在努力使用 UIDatePicker。我以为我可以像添加普通子视图一样将它添加到当前视图中,但它没有出现。经过一番搜索,我找到了将其放入弹出框的文档。我按照这些说明让它在 iPad 上工作。但是弹出框在 iPhone 上不起作用。我需要做什么才能让它在那里工作?为什么我不能像常规子视图一样简单地添加它?
这是我的 iPad 代码:
-(void)selectADate: (CGPoint)p
{
UIViewController* popoverContent = [[UIViewController alloc] init]; //ViewController
UIView *popoverView = [[UIView alloc] init]; //view
popoverView.backgroundColor = [UIColor blackColor];
UIDatePicker *datePicker=[[UIDatePicker alloc]init];//Date picker
datePicker.frame=CGRectMake(0,44,320, 216);
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker setTag:10];
datePicker.date = self.displayedDate;
[datePicker addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
[popoverView addSubview:datePicker];
popoverContent.view = popoverView;
UIPopoverController * popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
//popoverController.delegate=self;
self.datePopover = popoverController;
[popoverController setPopoverContentSize:CGSizeMake(320, 264) animated:NO];
CGRect popoverLocation = CGRectMake(p.x-160, p.y-200, 320, 216);
[popoverController presentPopoverFromRect:popoverLocation inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];//tempButton.frame where you need you can put that frame//
}
- (void)pickerChanged:(id)sender
{
self.displayedDate = [sender date];
[self showMonthContainingDate:[sender date]];
}
我的屏幕目前布局为一种显示一个月天数的日历页面。每天的销售都是一个子视图。屏幕顶部是一个标题,其中包含年、月等信息。当用户点击标题时,会出现日期选择器,日历页面显示与选择器中选择的日期对应的所选月份突出显示日期(当天的子视图的背景具有不同的颜色)。
我不确定为什么我什至需要一个弹出框,除非它只是为了提供一个空白视图,在该视图上放置日期选择器,该日期选择器位于我的日历页面的平铺视图前面。但是我不应该能够简单地将它添加为子视图,然后调用bringSubviewToFront:吗?我试过了,但它在 iPad 上不起作用,因此我使用了弹出框。
如果我在添加其他子视图时添加它,我可以让它显示,但我希望它在我点击标题时显示。这是我的点击代码:
- (IBAction)tap:(UITapGestureRecognizer *)sender {
//tap gesture recognizer
//Assume the user tapped on a day cell.
for (UIView *dayCell in self.view.subviews) {
if (CGRectContainsPoint(dayCell.frame, [sender locationInView:self.view])) {
//found the cell that was touched.
//NSLog(@"Found a view containing the touch with coords. %@",NSStringFromCGRect(dayCell.frame));
for (UIView *insideView in dayCell.subviews) {
for (UILabel *dayElement in insideView.subviews) {
//found a subview of the day cell.
if ([dayElement isKindOfClass:[UILabel class]]) {
NSLog(@"Found a subview %@",dayElement.text);
}
}
}
}
if ([dayCell isKindOfClass:[UILabel class]]) {
// This is the heading label.
UILabel * headingLabel = (UILabel *) dayCell;
if (CGRectContainsPoint(headingLabel.frame, [sender locationInView:self.view])) {
NSLog(@"The heading was tapped");
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
//iPhone code goes here
[self iphoneSelectADate:[sender locationInView:self.view]];
} else {
[self selectADate:[sender locationInView:self.view]];
}
}
}
}
}
这是当前无法使用的 iPhone 代码:
-(void)iphoneSelectADate: (CGPoint)p
{
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 216, 320, 216)];
[datePicker setDate:[NSDate date]]; //This is the default
[datePicker setHidden:NO];
[self.view addSubview:datePicker];
}
如果我在调试器中单步执行,我会看到此代码已执行,但日期选择器未显示。