我已經實現在彈出窗口中顯示日期選擇器,以編程方式執行它,如UIDatePicker in UIPopover中所示。如何使用StoryBoard在iPad上的Popover中顯示UIDatePicker?
但我一直在努力做在Interface Builder中,我已經在它和它對應的IBoulet取得了視圖控制器名爲DatePickerViewController.m有一個datepicker
#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)
}
而且我也導入自定義DatePickerViewController在我目前的ViewController
#import "DatePickerViewController.h"
但它沒有顯示的酥料餅裏面的日期選擇器。
有誰知道可能會發生?
請檢查我的回答https://stackoverflow.com/a/7343106/815179 – Narayana 2017-08-28 17:29:02