在我的应用程序中,我想显示UIDatePicker,当用户点击button.and该日期保存到UITextFiled.I做这些事情.
我的问题是当日期选择器出现没有完成按钮时,如何添加完成按钮.
现在我试过了
我的问题是当日期选择器出现没有完成按钮时,如何添加完成按钮.
现在我试过了
- (IBAction)pickerAction:(id)sender { datePicker.datePickerMode=UIDatePickerModeDate; datePicker.hidden=NO; datePicker.date=[NSDate date]; [datePicker addTarget:self action:@selector(TextTitle:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:datePicker]; NSDateFormatter * df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"M-d-yyyy"]; selectedDate.text=[df stringFromDate:datePicker.date]; } -(void)TextTitle:(id)sender { NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"M-d-yyyy"]; selectedDate.text = [NSString stringWithFormat:@"%@",[df stringFromDate:datePicker.date]]; }
解决方法
答案是
datePicker=[[UIDatePicker alloc]init]; datePicker.datePickerMode=UIDatePickerModeDate; [TextField1 setInputView:datePicker]; UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0,self.view.frame.size.width,44)]; [toolBar setTintColor:[UIColor grayColor]]; UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(ShowSelectedDate)]; UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; [toolBar setItems:[NSArray arrayWithObjects:space,doneBtn,nil]]; [TextField1 setInputAccessoryView:toolBar];