我已经实现了在弹出框内显示日期选择器,以编程方式执行它,如UIPopover 中的 UIDatePicker所示。
但是我一直在尝试在界面生成器中执行此操作,我已经创建了一个名为 DatePickerViewController.m 的视图控制器,其中包含一个 DatePicker 及其相应的 IBoulet
#import <UIKit/UIKit.h>
@interface DatePickerViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIDatePicker *birthdayDatePicker;
@end
然后我需要在编辑生日文本字段时将其显示在弹出窗口中。所以我使用以下代码
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
//Assign DatePicker to Birthday TextField
//build our custom popover view
DatePickerViewController* popoverContent = [[DatePickerViewController alloc] init];
//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 216);
// dismiss existing popover
if (self.popoverControllerBirthday)
{
[self.popoverControllerBirthday dismissPopoverAnimated:NO];
self.popoverControllerBirthday = nil;
}
//create a popover controller with my DatePickerViewController in it
UIPopoverController *popoverControllerForDate = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
//Set the delegate to self to receive the data of the Datepicker in the popover
popoverControllerForDate.delegate = self;
//Adjust the popover Frame to appear where I want
CGRect myFrame =textField.frame;
myFrame.origin.x = 260;
myFrame.origin.y = 320;
//Present the popover
[popoverControllerForDate presentPopoverFromRect:myFrame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionDown
animated:YES];
self.popoverControllerBirthday = popoverControllerForDate;
return NO; // tells the textfield not to start its own editing process (ie show the keyboard)
}
而且我还在我当前的 ViewController 中导入了自定义 DatePickerViewController
#import "DatePickerViewController.h"
但它没有显示弹出框内的日期选择器。
有人知道会发生什么吗?