我怎样才能显示了使用故事板在iPad上酥料饼内的UIDatePicker?(How can I sh

2019-06-27 03:13发布

我已经实现,以显示酥料饼里面的日期选择器,因为它显示在编程做的UIDatePicker在UIPopover 。

但我一直在努力做在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"

但它没有显示的酥料饼里面的日期选择器。

有谁知道可能会发生?

Answer 1:

您可能希望通过创建视图控制器和编程方式的DatePicker避免在脚本的所有工作:

//build our custom popover view
UIView *v = [[UIView alloc] init];
CGRect pickerFrame = CGRectMake(0, 0, 320, 216);
UIDatePicker *pView=[[UIDatePicker alloc] initWithFrame:pickerFrame];
pView.datePickerMode = UIDatePickerModeDate;

self.datePicker = pView;
[v addSubview:self.datePicker];

UIViewController *popoverContent = [[UIViewController alloc]init];
popoverContent.view = v;


//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 216);

// dismiss existing popover
if (self.pcDatePicker)
{
    [self.pcDatePicker dismissPopoverAnimated:NO];
    self.pcDatePicker = 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 = sender.frame;//CGRectMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 320,     200);
myFrame.origin.x = 260;
myFrame.origin.y = 320;

//Present the popover
[popoverControllerForDate presentPopoverFromRect:myFrame
                                          inView:self.view
                        permittedArrowDirections:UIPopoverArrowDirectionAny
                                        animated:YES];

self.pcDatePicker = popoverControllerForDate;

然后用委托方法来获得日期:

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
    self.startDate = self.datePicker.date;
}


Answer 2:

解决了

我所要做的就是实例故事板日期选取器视图控制器,如:

...
DatePickerViewController* popoverContent = [[DatePickerViewController alloc] init];

popoverContent =[[UIStoryboard storyboardWithName:@"MainStoryboard"
                                         bundle:nil]
               instantiateViewControllerWithIdentifier:@"DatePickerVC"];
//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 216);
...

其中标识符在故事板设置属性检查员

现在它显示



文章来源: How can I show a UIDatePicker inside a Popover on iPad using StoryBoard?