答案 0 :(得分:9)
我看到了您的链接并获得了解决方案,请尝试以下代码。
#import "TextfieldwithDatepickerViewController.h"
UIActionSheet *pickerViewPopup;
@implementation TextfieldwithDatepickerViewController
- (void)textFieldDidBeginEditing:(UITextField *)aTextField{
[aTextField resignFirstResponder];
pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerView.datePickerMode = UIDatePickerModeDate;
pickerView.hidden = NO;
pickerView.date = [NSDate date];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];
[barItems addObject:doneBtn];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
[barItems addObject:cancelBtn];
[pickerToolbar setItems:barItems animated:YES];
[pickerViewPopup addSubview:pickerToolbar];
[pickerViewPopup addSubview:pickerView];
[pickerViewPopup showInView:self.view];
[pickerViewPopup setBounds:CGRectMake(0,0,320, 464)];
}
-(void)doneButtonPressed:(id)sender{
//Do something here here with the value selected using [pickerView date] to get that value
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
}
-(void)cancelButtonPressed:(id)sender{
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
}
这段代码肯定对你有帮助,因为我运行代码并且工作正常。
答案 1 :(得分:2)
-(BOOL) textFieldShouldBeginEditing:(UITextField *)txtField
{
[self createDatePicker];
}
设置委托TextField
答案 2 :(得分:1)
您可以将日期选择器放在界面构建器中的另一个视图中......并将视图放在主视图下(x = 0; y = 460)。
因此,当你的textfield成为第一个响应者时(有一些委托方法,如textFieldDidBeginEditing :) ...只需使用datePicker在所需位置为视图设置动画。
动画可以像......一样完成。
[UIView beginAnimations:@"some_random_name" context:nil];
//give your date picker view a new frame
[UIView setAnimationDuration:0.3f]; //or any other value
[UIView commitAnimations];
答案 3 :(得分:1)
检查是否为文本字段设置了委托。 如果没有,请设置如下:
textField.delegate = self;
或者在xib中添加与委托的连接。
答案 4 :(得分:1)
我们可以使用UITextField的 inputView 和 inputAccessoryView 。 在.h文件中声明,在.m文件中声明@synthesize。
@property (nonatomic, retain) IBOutlet UIToolbar *_accessoryView;
@property (nonatomic, retain) IBOutlet UIDatePicker *_datePickerView;
将它们连接到.xib文件中的文件所有者,确保它不在self.view中。
- (void)viewDidLoad
{
_dateOfBirthTextField.inputView = self._datePickerView;
_dateOfBirthTextField.inputAccessoryView = self._toolbar;
}
还要编写函数以从选择器中获取日期
#pragma mark- UITextField with UIDatePicker-
- (IBAction)dateChanged:(id)sender {
UIDatePicker *picker = (UIDatePicker *)sender;
_dateOfBirthTextField.text = [NSString stringWithFormat:@"%@", picker.date];
}
- (IBAction)doneEditing:(id)sender {
[_dateOfBirthTextField resignFirstResponder];
}
在valueChanged上添加dateChanged:IBAction:_datePicker的动作。 和doneEditing:工具栏按钮操作上的IBAction。
答案 5 :(得分:0)
使用以下代码:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet showInView:self.tabBarController.tabBar];
[actionSheet setFrame:CGRectMake(0, 220, 320,350)];
pickerToolbar.barStyle = UIBarStyleDefault;
[pickerToolbar sizeToFit];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:[BuddyCalcAppDelegate getLocalvalue:@"Cancel"] style:UIBarButtonSystemItemCancel target:self action:@selector(cancel_clicked:)];
[barItems addObject:cancelBtn];
[cancelBtn release];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:[BuddyCalcAppDelegate getLocalvalue:@"Done"] style:UIBarButtonItemStyleDone target:self action:@selector(done_clicked:)];
[barItems addObject:doneBtn];
[doneBtn release];
[pickerToolbar setItems:barItems animated:YES];
[actionSheet addSubview:pickerToolbar];
[barItems release];
[pickerToolbar release];
UIDatePicker *datePicker=[[UIDatePicker alloc]init];
if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
{
datePicker.frame=CGRectMake(0,32,480, 216);
[scrollView setContentOffset:CGPointMake(0, 60) animated:YES];
} else {
datePicker.frame=CGRectMake(0,44,320, 216);
}
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[datePicker setMinuteInterval:5];
[datePicker setTag:10];
[actionSheet addSubview:datePicker];
[datePicker addTarget:self action:@selector(Result) forControlEvents:UIControlEventValueChanged];
[datePicker release];