我会使用 UILabel 来允许用户使用 UIDatePicker 选择日期。
为此,我创建了一个 UILabel 子类,覆盖了 inputView 和 inputAccessoryView 属性,使它们可写;我还实现了 -(BOOL) canBecomeFirstResponder 和 -(BOOL) isUserInteractionEnabled 方法,两者都返回 YES。然后我将一个 UIDatePIcker 实例分配给 inputView 属性。
在这一点上,我的期望是当标签被点击时,应该会出现一个 UIDatePicker,但什么也没有发生。
有什么帮助吗?
这是代码:
YPInteractiveUILabel.h
@interface YPInteractiveUILabel : UILabel
@property (readwrite) UIView *inputView;
@property (readwrite) UIView *inputAccessoryView;
- (BOOL) canBecomeFirstResponder;
- (BOOL) isUserInteractionEnabled;
@end
YPInteractiveUILabel.h
#import "YPInteractiveUILabel.h"
@implementation YPInteractiveUILabel
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
[self setInputView:datePicker];
}
return self;
}
- (BOOL)isUserInteractionEnabled
{
return YES;
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
@end