按钮上的日期选择器单击IOS

时间:2017-09-06 11:39:53

标签: ios objective-c

我知道这个问题被多次询问,但是当我点击按钮时,我面临一个问题,即我的日期选择器没有打开,选择之后我想在标签中显示它的值。我的代码是,

datepicker = [[UIDatePicker alloc]init];
datepicker.datePickerMode = UIDatePickerModeDate;

UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - 216 - 44, self.view.frame.size.width, 44)];
[toolBar setTintColor:[UIColor blackColor]];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(ShowSelectedDate)];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[toolBar setItems:[NSArray arrayWithObjects:doneBtn,space,nil]];

// _label1.text:datepicker.date;

-(void)ShowSelectedDate {
    NSDateFormatter *fromatter = [[NSDateFormatter alloc]init];
    [fromatter setDateFormat:@"dd/mm/yyyy"];
    _label1.text = [NSString stringWithFormat:@"%@",[fromatter stringFromDate:datepicker.date]];
    [_label1 resignFirstResponder];
}

4 个答案:

答案 0 :(得分:4)

您可以为按钮创建一个IBAction来调用以显示UIDatePicker。然后实现UIDatePicker委托方法并设置标签文本。

目标C

全球定义

UIToolbar* toolbar;
UIDatePicker* picker;

在您的标头文件中:

-(IBAction)showDatePicker:(id)sender;

在您的实施文件中

- (IBAction)showDatePicker:(id)sender {
    picker = [[UIDatePicker alloc] init];
    picker.backgroundColor = [UIColor whiteColor];
    [picker setValue:[UIColor blackColor] forKey:@"textColor"];

    picker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    picker.datePickerMode = UIDatePickerModeDate;

    [picker addTarget:self action:@selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged];
    picker.frame = CGRectMake(0.0, [UIScreen mainScreen].bounds.size.height - 300, [UIScreen mainScreen].bounds.size.width, 300);
    [self.view addSubview:picker];

    toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 300, [UIScreen mainScreen].bounds.size.width, 50)];
    toolbar.barStyle = UIBarStyleBlackTranslucent;
    toolbar.items = @[[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(onDoneButtonClick)]];
    [toolbar sizeToFit];
    [self.view addSubview:toolbar];
}

-(void) dueDateChanged:(UIDatePicker *)sender {

    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterLongStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];

    NSLog(@"Picked the date %@", [dateFormatter stringFromDate:[sender date]]);
    YOUR_LABEL.TEXT = [dateFormatter stringFromDate:[sender date]];
}

-(void)onDoneButtonClick {
    [toolbar removeFromSuperview];
    [picker removeFromSuperview];
}

Swift 4.2

全球定义

var toolBar = UIToolbar()
var picker  = UIDatePicker()

在您的按钮操作中编写代码。

@IBAction func showDatePicker(_ sender: UIButton) {
    datePicker = UIDatePicker.init()
    datePicker.backgroundColor = UIColor.white

    datePicker.autoresizingMask = .flexibleWidth
    datePicker.datePickerMode = .date

    datePicker.addTarget(self, action: #selector(self.dateChanged(_:)), for: .valueChanged)
    datePicker.frame = CGRect(x: 0.0, y: UIScreen.main.bounds.size.height - 300, width: UIScreen.main.bounds.size.width, height: 300)
    self.view.addSubview(datePicker)

    toolBar = UIToolbar(frame: CGRect(x: 0, y: UIScreen.main.bounds.size.height - 300, width: UIScreen.main.bounds.size.width, height: 50))
    toolBar.barStyle = .blackTranslucent
    toolBar.items = [UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil), UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.onDoneButtonClick))]
    toolBar.sizeToFit()
    self.view.addSubview(toolBar)
}

@objc func dateChanged(_ sender: UIDatePicker?) {
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .long
    dateFormatter.timeStyle = .none

    if let date = sender?.date {
        print("Picked the date \(dateFormatter.string(from: date))")
    }
}

@objc func onDoneButtonClick() {
    toolBar.removeFromSuperview()
    datePicker.removeFromSuperview()
}

答案 1 :(得分:0)

试试这段代码,

 -(IBAction)showDatePicker:(id)sender
    {

    UIDatePicker* picker = [[UIDatePicker alloc] init];
                        picker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
                        picker.datePickerMode = UIDatePickerModeDate;

 [picker addTarget:self action:@selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged];
                        CGSize pickerSize = [picker sizeThatFits:CGSizeZero];
                        picker.frame = CGRectMake(0.0, 250, pickerSize.width, 460);
                        picker.backgroundColor = [UIColor blackColor];
                        [self.view addSubview:picker];
                        [picker release];


    }

-(void) dueDateChanged:(UIDatePicker *)sender {
    NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateStyle:NSDateFormatterLongStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];

    //self.myLabel.text = [dateFormatter stringFromDate:[dueDatePickerView date]];
    NSLog(@"Picked the date %@", [dateFormatter stringFromDate:[sender date]]);
}

答案 2 :(得分:0)

您尚未在代码中的任何位置将datePicker添加到视图中。  试试这个

datepicker=[[UIDatePicker alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - 216, self.view.frame.size.width, 216)];
datepicker.datePickerMode=UIDatePickerModeDate;

[self.view addSubview:picker]; // adding picker to your view

此外,您还没有在视图中添加toolbar。所以,这样做

[self.view addSubview:toolbar];

希望有所帮助......

答案 3 :(得分:0)

尽管问题是针对Objective-C,但我对 Swift 5.0

的回答
private lazy var datePicker : UIDatePicker = {
        let datePicker = UIDatePicker()
        datePicker.translatesAutoresizingMaskIntoConstraints = false
        datePicker.autoresizingMask = .flexibleWidth
        if #available(iOS 13, *), self.view.isDarkModeEnabled() {
            datePicker.backgroundColor = .label
        } else {
            datePicker.backgroundColor = .white
        }
        datePicker.datePickerMode = .date
        datePicker.addTarget(self, action: #selector(self.dateChanged), for: .valueChanged)
        return datePicker
    }()
    
    private lazy var toolBar : UIToolbar = {
        let toolBar = UIToolbar()
        toolBar.translatesAutoresizingMaskIntoConstraints = false
        toolBar.barStyle = .default
        toolBar.items = [UIBarButtonItem.init(barButtonSystemItem: .flexibleSpace, target: nil, action: nil), UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.onDoneClicked))]
        toolBar.sizeToFit()
        return toolBar
    }()
    
@objc private func onDoneClicked(picker : UIDatePicker) {
        
    }
    
    @objc private func dateChanged(picker : UIDatePicker) {
        
    }

private func addDatePicker() {
        self.view.addSubview(self.datePicker)
        self.view.addSubview(self.toolBar)
        
        NSLayoutConstraint.activate([
            self.datePicker.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
            self.datePicker.leftAnchor.constraint(equalTo: self.view.leftAnchor),
            self.datePicker.rightAnchor.constraint(equalTo: self.view.rightAnchor),
            self.datePicker.heightAnchor.constraint(equalToConstant: 300)
        ])
        
        NSLayoutConstraint.activate([
            self.toolBar.bottomAnchor.constraint(equalTo: self.datePicker.topAnchor),
            self.toolBar.leftAnchor.constraint(equalTo: self.view.leftAnchor),
            self.toolBar.rightAnchor.constraint(equalTo: self.view.rightAnchor),
            self.toolBar.heightAnchor.constraint(equalToConstant: 50)
        ])
    }