I would use a UILabel to allow users to select a date with UIDatePicker.
To do this, I created an UILabel subclass overwriting the inputView and the inputAccessoryView properties making them writable; I also implemented the -(BOOL) canBecomeFirstResponder and the -(BOOL) isUserInteractionEnabled methods returning YES for both. Then I assigned an instance of UIDatePIcker to the inputView property.
At this point my expectation is that when the label is tapped an UIDatePicker should appear, but nothing happens.
Any help?
This is the code:
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
See Question&Answers more detail:os