UILabel doesn't show inputView

前端 未结 7 1293
长发绾君心
长发绾君心 2020-12-18 07:14

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 inputAccessoryVi

7条回答
  •  长情又很酷
    2020-12-18 07:32

    I know this is an old question but this might still be useful to someone.

    There is another way to solve this - there is no need to complicate things with gesture recognizers...

    GTPDateLabel.h

    @interface GTPDateLabel : UILabel
    
    @property (readonly, retain) UIView *inputView;
    
    @end
    

    GTPDateLabel.m

    #import "GTPDateLabel.h"
    
    @implementation GTPDateLabel
    
    @synthesize inputView = _inputView;
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
    
        if (self)
        {
            self.userInteractionEnabled = YES;
        }
    
        return self;
    }
    
    - (id)initWithCoder:(NSCoder *)aDecoder
    {
        self = [super initWithCoder:aDecoder];
    
        if (self)
        {
            self.userInteractionEnabled = YES;
        }
    
        return self;
    }
    
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [super touchesEnded:touches withEvent:event];
    
        [self becomeFirstResponder];
    }
    
    - (BOOL)canBecomeFirstResponder
    {
        return YES;
    }
    
    -(UIView *)inputView
    {
        if (!_inputView)
        {
            UIDatePicker *datePicker = [[UIDatePicker alloc] init];
    
            _inputView = datePicker;
        }
    
        return _inputView;
    }
    
    @end
    

    Note that you should also set the delegate and in case of custom UIPicker also dataSource...

提交回复
热议问题