Objective C implementing a UIPickerView with a “Done” button

后端 未结 7 598
一向
一向 2020-12-03 08:56

I am trying to implement a \"Done\" button in a UIPickerView Similar to the one under this link

I looked in the class reference but I couldn t find it

Thank

相关标签:
7条回答
  • 2020-12-03 09:47

    There is a more elegant solution. I'm not sure if this is recent (as of iOS7), but this has been working for me splendidly.

    TJDatePicker.h

    @protocol TJDatePickerActionDelegate <NSObject>
    - (void)cancel:(id)sender;
    - (void)done:(id)sender;
    @end
    
    @interface TJDatePicker : UIDatePicker
    @property (strong, nonatomic) UIView *navInputView;
    @property (weak, nonatomic) id<TJDatePickerActionDelegate> actionDelegate;
    @end
    

    TJDatePicker.m

    #import "TJDatePicker.h"
    
    @interface TJDatePicker ()
    @property (strong, nonatomic) TJButton *cancelButton;
    @property (strong, nonatomic) TJButton *doneButton;
    @end
    
    @implementation TJDatePicker
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self)
        {
            [self updateSubviews];
        }
    
        return self;
    }
    
    - (void)layoutSubviews
    {
        [super layoutSubviews];
    
        [self updateSubviews];
    }
    
    - (void)updateSubviews
    {
        self.navInputView.frame = CGRectMake(0, 0, self.width, 45);
        self.cancelButton.frame = CGRectMake(5, 5, 80, 35);
        CGFloat width = 80;
        self.doneButton.frame = CGRectMake(CGRectGetMaxX(self.navInputView.frame) - width, self.cancelButton.frame.origin.y, width, self.cancelButton.height);
    }
    
    - (UIView *)navInputView
    {
        if (!_navInputView)
        {
            _navInputView = [[UIView alloc] init];
            _navInputView.backgroundColor = [UIColor whiteColor];
    
            self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.cancelButton setTitle:@"CANCEL" forState:UIControlStateNormal];
            [self.cancelButton addTarget:self action:@selector(cancelButtonPressed) forControlEvents:UIControlEventTouchUpInside];
            [_navInputView addSubview:self.cancelButton];
    
            self.doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.doneButton setTitle:@"DONE" forState:UIControlStateNormal];
            [self.doneButton addTarget:self action:@selector(doneButtonPressed) forControlEvents:UIControlEventTouchUpInside];
            [_navInputView addSubview:self.doneButton];
        }
    
        return _navInputView;
    }
    
    - (void)cancelButtonPressed
    {
        [self.actionDelegate cancel:self];
    }
    
    - (void)doneButtonPressed
    {
        [self.actionDelegate done:self];
    }
    

    And then at implementation time...

    self.datePicker = [[TJDatePicker alloc] init];
    self.datePicker.actionDelegate = self;
    self.textField.inputAccessoryView = self.datePicker.navInputView;
    

    The key here is to use the inputAccessoryView of the UITextField that you are planning on setting the UIDatePicker to.

    0 讨论(0)
提交回复
热议问题