2015-06-19 17 views
13

Tôi đang cố gắng triển khai UIToolbar trên UIDatepicker để loại bỏ nó khi chạm vào nút "Xong". Nhưng khi tôi bấm vào nút, các datepicker là di chuyển và nút hành động không hoạt động.UIDatePicker với UIToolbar

Đây là mã của tôi:

datepicker = [[UIDatePicker alloc] initWithFrame:CGRectZero]; 
[datepicker setDatePickerMode:UIDatePickerModeDate]; 
[datepicker addTarget:self action:@selector(changeDate:) forControlEvents:UIControlEventValueChanged]; 

UIToolbar *toolbar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,44)]; 
toolbar.barStyle = UIBarStyleDefault; 
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismiss)]; 

[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]]; 
[datepicker addSubview:toolbar]; 

datepicker của tôi:

UIDatePicker

Cảm ơn bạn đã giúp đỡ của bạn.

+1

bạn đã thực hiện phương pháp 'loại bỏ' chưa? –

Trả lời

21

Nếu bạn sử dụng UITextField thiết

textField.inputAccessoryView = toolbar; 
textField.inputView = datepicker; 
2

Bạn phải thiết lập inputAccessoryViewInputView để để bạn UITextField

txtField.inputAccessoryView = toolBar; 
txtField.inputView = datePickerView; 
0
remove this line from your code: 
[datepicker addSubview:toolbar]; 
and add dismiss method nd add 
txtCurrent.inputAccessoryView = toolbar; in didbeginEditing. 

-(void)textFieldDidBeginEditing:(UITextField *)textField { 

    txtCurrent = textField; 
    [datePicker setHidden:NO]; 
    txtCurrent.inputAccessoryView = toolbar; 
    if ([textField.text isEqualToString:@""]) { 

     NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
     NSDate *eventDate = [NSDate date]; 
     [dateFormat setDateFormat:@"MM/dd/yyyy"]; 
     NSString *dateString = [dateFormat stringFromDate:eventDate]; 
     textField.text = [NSString stringWithFormat:@"%@",dateString]; 
     } 
     [textField setInputView:datePicker]; 
} 
-(void)dismiss{ 
    datePicker.hidden = YES; 
    [self.view endEditing:YES]; 

} 
0

Hãy thử điều này [self.view addSubview:toolbar];

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

[picker addTarget:self action:@selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged]; 
//CGSize pickerSize = [picker sizeThatFits:CGSizeZero]; 
picker.frame = CGRectMake(0.0, self.view.frame.size.height - 250, self.view.frame.size.width, 250); 
picker.backgroundColor = [UIColor whiteColor]; 

toolbar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height - 294,self.view.frame.size.width,44)]; 
toolbar.barStyle = UIBarStyleDefault; 
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 

UIButton* infoButton = [UIButton buttonWithType: UIButtonTypeInfoLight]; 
[infoButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchDown]; 

UIBarButtonItem* doneButton =[[UIBarButtonItem alloc]initWithCustomView:infoButton]; 



[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]]; 


[self.view addSubview:toolbar]; 


[self.view addSubview:picker]; 
Các vấn đề liên quan