objective-c – 如何通过StoryBoard在DatePicker中添加“完成”按钮?

前端之家收集整理的这篇文章主要介绍了objective-c – 如何通过StoryBoard在DatePicker中添加“完成”按钮?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的应用程序中,我想显示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];

猜你在找的C&C++相关文章