我会使用UILabel来允许用户使用UIDatePicker选择日期。UILabel不显示inputView
要做到这一点,我创建了一个子类的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
尝试在init方法中添加self.userInteractionEnabled = YES。 – jsd
为什么不使用UITextField呢? – rdelmar
@jsd我试过了,但它不起作用。 – massyc