UILabel doesn't show inputView

前端 未结 7 1291
长发绾君心
长发绾君心 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:30

    How about something like this. Rather than subclass the label, just add a gesture recognizer to it, and bring up the picker in the tap recognizer's handler. In the picker's action method, populate the label and dismiss the picker. This example works, but you'd probably want to add some animation to make it look better:

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.label.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(launchPicker:)];
        [self.label addGestureRecognizer:tapper];
    }
    
    -(void)launchPicker:(UITapGestureRecognizer *) tapper {
        UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(5, 150, 300, 200)];
        [picker addTarget:self action:@selector(updateLabel:) forControlEvents:UIControlEventValueChanged];
        [self.view addSubview:picker];
    }
    
    -(IBAction)updateLabel:(UIDatePicker *)sender {
        self.label.text = [NSString stringWithFormat:@"%@",sender.date ];
        [sender removeFromSuperview];
    }
    

提交回复
热议问题