如何在iPad中显示UIDatePicker?
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您的问题更多地与学习有关,而不是特定的疑问或问题,因此我发布了教程链接。这 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
如果您希望在单击文本字段时显示弹出窗口,则您正在使用正确的方法进行操作,
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
。问题是这是一个委托方法textField 因此您应该在 .h 文件的界面中包含UITextFieldDelegate
并设置文本字段的委托,如下所示
这应该可以解决您的问题。
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 theUITextFieldDelegate
in you interface in the .h fileand set the delegate of you text field like this
That should solve your problem.