XCode:当用户单击 UITextbox 时显示 UIDatePicker
我已经对这个话题进行了彻底的研究,发现人们在许多网站上发布了完全相同的问题,包括这里 在 stackoverflow 中。
我已尝试了所有建议,但无法让 UIDatePicker 实际显示。我采取什么方法似乎并不重要。我尝试使用继承模型,在该模型中,我对 UITextBox 控件进行子类化并重写它的默认方法,以便显示 UIDatePicker,然后确保在 StoryBoard 中将 UITextView 控件的类设置为该自定义类。我尝试以编程方式生成 UIDatePicker 并尝试将其拖动到 StoryBoard 中的视图上。当我尝试编程方法时,不会显示任何内容,而当我尝试将其拖动到故事板时,它始终会显示。当我将其属性设置为“隐藏”时,它会隐藏,但即使我尝试将代码添加到应该取消隐藏它的 textboxDidBeginEditing
方法,我也无法显示它。 我已确保将 UITextView 的 inputView 属性设置为等于我的 UIDatePicker 控件。
什么都不起作用!我不明白为什么 Apple 不只是将 UIDatePicker 作为 UITextView 控件属性检查器下拉列表中的默认选项之一。那会让事情变得容易得多。
下面是我的实现类文件中的一些代码。
#import "AddTasksViewController.h"
@implementation AddTasksViewController
@synthesize dueDate;
@synthesize description;
@synthesize shortTitle;
@synthesize datePicker;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.dueDate.inputView = datePicker;
}
return self;
}
- (IBAction)dueDateDidBeginEditing:(UITextView *)textField
{
[textField resignFirstResponder];
[datePicker setFrame:CGRectMake(0,200,320,120)];
[datePicker addTarget:self action:@selector(done) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
}
这是我的头文件的样子...
#import <UIKit/UIKit.h>
@interface AddTasksViewController : UIViewController
@property (nonatomic, copy) IBOutlet UITextView *dueDate;
@property (nonatomic, copy) IBOutlet UITextView *description;
@property (nonatomic, copy) IBOutlet UITextView *shortTitle;
@property (nonatomic, retain) IBOutlet UIDatePicker *datePicker;
- (IBAction)doneEditing:(id)sender;
- (IBAction)dateChanged:(id)sender;
- (IBAction)dueDateDidBeginEditing:(UITextView *)textField;
@end
正如我已经提到的,我采取的另一种方法(即子类化 UITextView)也不起作用。我完全按照显示的方式复制了此处提供的代码。然后,我将名为“DateField”的自定义类添加到我的项目(头文件和实现文件)中,然后在 AddTaskViewController.h 和 AddTaskViewController.m 文件中用 DateField 替换 dueDate 的 UITextBox 声明,并确保 StoryBoard 引用通过在身份检查器中为控件的类选择 DateField 来更新。
无论我做什么,我都无法在单击 UITextView 时显示 UIDatePicker。当我设置断点时,它确实命中了 dueDateDidBeginEditing 方法,因此我知道正在触发某些内容。问题是我不明白为什么 UIDatePicker 没有显示子视图。
任何帮助将不胜感激,因为这项看似简单的任务似乎花费的时间比应有的要长得多。在 Java 和 C# 中,我可以闭着眼睛、双手被绑在背后做这样的事情,但当涉及到 Objective-C 时,一切都变得不必要的复杂。语法真的让我很恼火。
这么多方括号!!!啊啊啊!!!
I have already researched this topic to death and found people posting the exact same question on a number of websites including right here in stackoverflow.
I have tried all of the suggestions but have been unable to get the UIDatePicker to actually display. It doesn't seem to matter what approach I take. I've tried using the inheritance model where I subclass the UITextBox control and override it's default methods in order to display the UIDatePicker and then I make sure that in StoryBoard I set the class of my UITextView control to be that custom class. I've tried programmatically generating the UIDatePicker and also tried dragging it onto the view in StoryBoard. When I try the programmatic approach nothing is displayed and when I try dragging it onto the StoryBoard it ALWAYS displays. When I set it's attribute to "hidden" it hides but then I can't get it to show even when I try to add code to the textboxDidBeginEditing
method that should unhide it.
I've made sure to set the UITextView's inputView property equal to my UIDatePicker control.
Nothing works! I don't understand why Apple didn't just make the UIDatePicker one of the default options in the drop down list in the Attributes Inspector for the UITextView control. That would have made this so much easier.
Below is some of the code from my implementation class file.
#import "AddTasksViewController.h"
@implementation AddTasksViewController
@synthesize dueDate;
@synthesize description;
@synthesize shortTitle;
@synthesize datePicker;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.dueDate.inputView = datePicker;
}
return self;
}
- (IBAction)dueDateDidBeginEditing:(UITextView *)textField
{
[textField resignFirstResponder];
[datePicker setFrame:CGRectMake(0,200,320,120)];
[datePicker addTarget:self action:@selector(done) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
}
Here is what my header file looks like...
#import <UIKit/UIKit.h>
@interface AddTasksViewController : UIViewController
@property (nonatomic, copy) IBOutlet UITextView *dueDate;
@property (nonatomic, copy) IBOutlet UITextView *description;
@property (nonatomic, copy) IBOutlet UITextView *shortTitle;
@property (nonatomic, retain) IBOutlet UIDatePicker *datePicker;
- (IBAction)doneEditing:(id)sender;
- (IBAction)dateChanged:(id)sender;
- (IBAction)dueDateDidBeginEditing:(UITextView *)textField;
@end
As I've already mentioned, the other approach that I took (i.e. subclassing UITextView) didn't work either. I copied the code provided here exactly as it was show. I then added the custom classes called "DateField" to my project (both the header and implementation files) and then replaced the UITextBox declaration for the dueDate with DateField in both my AddTaskViewController.h and AddTaskViewController.m files and made sure that the StoryBoard references were updated to by selecting DateField for the control's class in the Identity Inspector.
No matter what I do I cannot get the UIDatePicker to display upon clicking on the UITextView. When I set a breakpoint it does hit the dueDateDidBeginEditing
method so I know that something is being triggered. The problem is that I don't understand why a subView is not being showing with the UIDatePicker.
Any help would be greatly appreciated as this seemingly straightforward task seems to be taking much, much longer than it should. I could do this sort of stuff with my eyes closed and hands tied behind my back in Java and C# yet everything is so unnecessarily complicated when it comes to Objective-C. The syntax really irritates me.
So many square brackets!!! Arghhhh!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
与显示键盘相比,为
UITextField
的inputView
显示UIDatePicker
相对困难,但与您遇到的麻烦相比相对容易。我在您的代码中注意到的第一个问题之一是,您正在尝试设置尚不存在的对象的属性,这对于 iOS/Mac 开发新手来说是一个非常常见的问题。将“
self.dueDate.inputView = datePicker;
”放入initWithNibName:bundle:
将不起作用,因为视图尚未加载,并且dueDate
是您观点的一部分。在 Objective-C 中,对象是通过 alloc & 实例化的。初始化。 Init 是对任何对象的第一个真正的方法调用。此时,在视图控制器生命周期中,视图尚未创建。self.dueDate.inputView = datePicker;
所属的方法调用位于viewDidLoad
方法中。它在听起来像被调用时被准确地调用,并且您的 dueDate 属性将在此时正确加载。无需使用UITextField
的自定义子类来显示文本字段的日期选择视图。这是自定义输入视图类的一个非常基本的示例:ExampleBasicDateInputView.h:
ExampleBasicDateInputView.m:
然后在视图加载时,您将像这样使用此类:
正如您在
.h
文件中看到的那样我们已将日期选择器实例公开为属性,以便您可以设置样式。等等:显然这是一个非常基本的例子,但我认为它展示了可以做什么。
Showing a
UIDatePicker
for theinputView
of aUITextField
is relatively hard compared to showing a keyboard, but relatively easy compared to the trouble you've had.One of the first problems I noticed with your code, and it's a very common problem with those new to iOS/Mac development, is that you are attempting to set the property of an object that doesn't exist yet. Putting "
self.dueDate.inputView = datePicker;
" in yourinitWithNibName:bundle:
will not work because the view has not yet loaded, anddueDate
is part of your view. In objective-c an object is instantiated with alloc & init. Init is the first real method call to any object. At this point in your view controller's life the view has not been created yet. The method call whereself.dueDate.inputView = datePicker;
belongs is in theviewDidLoad
method. It is called exactly when it sounds like it's called, and yourdueDate
property will be properly loaded at that point. There is no need to use a custom subclass ofUITextField
to display a date picking view for for your text field. Here is a very basic example of a custom input view class:ExampleBasicDateInputView.h:
ExampleBasicDateInputView.m:
Then in view did load you would use this class like this:
And as you see in the
.h
file we've exposed the date picker instance as a property so you can set the style. etc. like so:Obviously this is a very basic example, but I think it shows what can be done.
我在使用故事板时遇到了同样的问题。以下是我解决该问题的方法:
概述:
我在 viewDidLoad: 中分配并初始化我的 UIDatePicker 属性。然后,我将文本字段的 inputView 属性设置为 datepicker 属性。
这是我的代码:
在 TripViewController.h 中:
在 TripViewController.m 中:
然后,我实现我的方法 dateChanged,以便每当日期选择器轮停止移动时,我的文本字段就会更新。
I had the same issue using Storyboard. Here is how I resolved it:
Overview:
I alloc and initialize my UIDatePicker property in viewDidLoad:. I then set the inputView property of my textfield to my datepicker property.
Here is my code:
In TripViewController.h:
In TripViewController.m:
I then implement my method, dateChanged so that whenever the date picker wheel stops moving, my textfield is updated.
在 .h 文件中添加 UITextField 的委托,以便在触摸时调用此函数。然后使用文本字段的标记值来确定选择该字段时要执行的操作。
add UITextField's delegate in .h file to call this function when touched. then use the text field's tag value to determine what you want to do when the field is selected.
这很容易。在 viewDidLoad 的 .h 中定义委托 TextField (UITextFieldDelegate) 。 m
在同一个 viewDidLoad 中将 datePicker 关联到 myTextField。
你也可以做动画,但这是最简单的方法。
It is very easy. Defines delegate TextField (UITextFieldDelegate) in the .h in the viewDidLoad .m
In the same viewDidLoad associates the datePicker to myTextField.
You can also do anime, but this is the easiest way.