iPhone: Adding a Done button within a pop up DatePicker frame

前端 未结 5 853
我寻月下人不归
我寻月下人不归 2020-12-17 05:12

I pop up a DatePicker with the following. Now I\'m trying to add a Done button at the top of the pop up frame.

-(IBAction) contactBDayDatePicker{

NSLog(@         


        
5条回答
  •  遥遥无期
    2020-12-17 05:23

    I customize Micah's class to support multiple orientation and ARC

    DateTimePicker.h

        @interface DateTimePicker : UIView {
    }
    
    @property (nonatomic, assign, readonly) UIDatePicker *picker;
    
    - (void) setMode: (UIDatePickerMode) mode;
    - (void) addTargetForDoneButton: (id) target action: (SEL) action;
    
    @end
    

    DateTimePicker.m

    #define MyDateTimePickerToolbarHeight 40
    
    @interface DateTimePicker()
    
    @property (nonatomic, assign, readwrite) UIDatePicker *picker;
    
    @property (nonatomic, assign) id doneTarget;
    @property (nonatomic, assign) SEL doneSelector;
    
    - (void) donePressed;
    
    @end
    
    
    @implementation DateTimePicker
    
    @synthesize picker = _picker;
    
    @synthesize doneTarget = _doneTarget;
    @synthesize doneSelector = _doneSelector;
    
    - (id) initWithFrame: (CGRect) frame {
        if ((self = [super initWithFrame: frame])) {
            self.backgroundColor = [UIColor clearColor];
    
            UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame: CGRectMake(0, MyDateTimePickerToolbarHeight, frame.size.width, frame.size.height - MyDateTimePickerToolbarHeight)];
            [self addSubview: picker];
    
            UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, frame.size.width, MyDateTimePickerToolbarHeight)];
            toolbar.barStyle = UIBarStyleBlackOpaque;
            toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    
            UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle: @"Done" style: UIBarButtonItemStyleBordered target: self action: @selector(donePressed)];
            UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
            toolbar.items = [NSArray arrayWithObjects:flexibleSpace, doneButton, nil];
    
            [self addSubview: toolbar];
    
            self.picker = picker;
            picker.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
    
            self.autoresizesSubviews = YES;
            self.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
        }
        return self;
    }
    
    - (void) setMode: (UIDatePickerMode) mode {
        self.picker.datePickerMode = mode;
    }
    
    - (void) donePressed {
        if (self.doneTarget) {
            [self.doneTarget performSelector:self.doneSelector withObject:nil afterDelay:0];
        }
    }
    
    - (void) addTargetForDoneButton: (id) target action: (SEL) action {
        self.doneTarget = target;
        self.doneSelector = action;
    }
    

    Using custom view in your view controller:

    NSDate *selectedDate;  
    UIButton *button;
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        selectedDate = [NSDate new];
    
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"MM/dd/yyyy";
    
        button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button addTarget:self
                   action:@selector(buttonPressed:)
         forControlEvents:UIControlEventTouchDown];
        [button setTitle:[dateFormatter stringFromDate:selectedDate] forState:UIControlStateNormal];
        button.frame = CGRectMake(100, 50, 100, 40.0);
        [self.view addSubview:button];
    
        CGRect screenRect = [[UIScreen mainScreen] bounds];
        CGFloat screenWidth = screenRect.size.width;
        CGFloat screenHeight = screenRect.size.height;
        picker = [[DateTimePicker alloc] initWithFrame:CGRectMake(0, screenHeight/2 - 35, screenWidth, screenHeight/2 + 35)];
        [picker addTargetForDoneButton:self action:@selector(donePressed)];
        [self.view addSubview:picker];
        picker.hidden = YES;
        [picker setMode:UIDatePickerModeDate];
        [picker addTarget:self action:@selector(pickerChanged) forControlEvents:UIControlEventValueChanged];
    }
    
    -(void)pickerChanged {  
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"MM/dd/yyyy";
    
        selectedDate = picker.date;
        [button setTitle:[dateFormatter stringFromDate:selectedDate] forState:UIControlStateNormal];
    }    
    
    -(void)donePressed {
        picker.hidden = YES;
    }
    
    -(void)buttonPressed:(id)sender {
        picker.hidden = NO;
        [picker setDate:selectedDate]; 
    }
    

提交回复
热议问题