如何在iPad中显示UIDatePicker?

发布于 2024-12-19 07:30:20 字数 1321 浏览 1 评论 0原文

我在 iPhone 中实现了 UIDatePicker,它工作正常。

我在 iPad 上尝试了相同的代码,但它没有向我显示 UIDatePicker

我在某处读到我需要用 UIPopOverController 显示它并找到 这个 链接,但它没有显示正确的代码。

我无法为此创建一个单独的 XIB。我必须通过代码本身来实现它。 我的是IOS5。

我使用以下代码:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField.tag == 1)
    {
            [self showDatePickerIniPad];
      }
}
   -(void)showDatePickerIniPad
 {
     UIViewController *popoverContent = [[UIViewController alloc] init];
     UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
     popoverView.backgroundColor = [UIColor whiteColor];
     datePicker.frame = CGRectMake(0, 0, 320, 300);
     [popoverView addSubview:datePicker];
     popoverContent.view = popoverView;
     popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 244);

     UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
     [popoverController presentPopoverFromRect:CGRectZero inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

     txt_FromDate.inputView = datePicker;
 }

I implemented the UIDatePicker in iPhone and it's working correctly.

The same code I tried with iPad but it is not showing me UIDatePicker.

I read somewhere that I need to show it with UIPopOverController and find this
link but it is not showing the proper code.

I can't make a separate XIB for that. I have to implement it by code itself.
Mine is IOS5.

I use the following code:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField.tag == 1)
    {
            [self showDatePickerIniPad];
      }
}
   -(void)showDatePickerIniPad
 {
     UIViewController *popoverContent = [[UIViewController alloc] init];
     UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
     popoverView.backgroundColor = [UIColor whiteColor];
     datePicker.frame = CGRectMake(0, 0, 320, 300);
     [popoverView addSubview:datePicker];
     popoverContent.view = popoverView;
     popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 244);

     UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
     [popoverController presentPopoverFromRect:CGRectZero inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

     txt_FromDate.inputView = datePicker;
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

方觉久 2024-12-26 07:30:20

由于您的问题更多地与学习有关,而不是特定的疑问或问题,因此我发布了教程链接。这 2 个教程应该可以解决您的问题 -

UIPickerView 基础知识

<一个href="http://www.techotopia.com/index.php/Using_the_UIPickerView_and_UIDatePicker_Components_in_iOS_4_iPad_Applications_%28Xcode_4%29" rel="nofollow noreferrer">UIDateView 组件

更新:还要检查这些高度相关的内容来自 stackoverlflow 本身的链接 -

iPad 上 UIActionSheet 中的 UIDatePicker

https://stackoverflow.com/questions/5830585/uidatepicker-inside-uiactionsheet-ipad

iPad 上的空白 UIDatePicker 模式为 UIDatePickerModeDateAndTime

Since your question is more on the lines of learning rather than a specific doubt or question, i am posting tutorial links. These 2 tutorials should solve your problem(s) -

UIPickerView Basics

UIDateView Components

UPDATE: Also check these highly relevant links from stackoverlfow itself -

UIDatePicker in UIActionSheet on iPad

https://stackoverflow.com/questions/5830585/uidatepicker-inside-uiactionsheet-ipad

blank UIDatePicker on iPad in mode UIDatePickerModeDateAndTime

南冥有猫 2024-12-26 07:30:20

如果您希望在单击文本字段时显示弹出窗口,则您正在使用正确的方法进行操作,- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField。问题是这是一个委托方法textField 因此您应该在 .h 文件的界面中包含 UITextFieldDelegate

@interface myViewController : UIViewController<UITextFieldDelegate>{

并设置文本字段的委托,如下所示

self.yourTextField.delegate = self;

这应该可以解决您的问题。

If you want the popover to show when you click on the textfield you are doing in the right method, - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField.The problem is this is a Delegate Method for the textField so you should include the UITextFieldDelegate in you interface in the .h file

@interface myViewController : UIViewController<UITextFieldDelegate>{

and set the delegate of you text field like this

self.yourTextField.delegate = self;

That should solve your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文