我有一个UITextField,我将inputView设置为UIDatePicker,这样当按下UITextField而不是键盘时,会弹出UIDatePicker。我还想在其上添加一个带有“下一步”按钮的工具栏,以便用户可以轻松跳转到下一个字段,但我不知道如何执行此操作,因为我已经在UITextField上更改视图以获取的UIDatePicker。以下是加载视图时使用的代码:
- (void)viewDidLoad
{
self.datePicker = [[UIDatePicker alloc] init];
self.datePicker.datePickerMode = UIDatePickerModeDate;
[self.datePicker addTarget:self action:@selector(datePickerValueChanged) forControlEvents:UIControlEventValueChanged];
issue.inputView = self.datePicker;
}
将“下一步”按钮添加到工具栏顶部的建议是什么?
答案 0 :(得分:4)
创建一个视图以用作文本字段的inputAccessoryView。此视图应包含您的按钮(例如,下一步,取消,上一页)。
答案 1 :(得分:3)
以下是代码:
if (keyboardToolbar == nil) {
keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
[keyboardToolbar setBarStyle:UIBarStyleBlackTranslucent];
UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleDone target:self action:@selector(switchToNextField:)];
[keyboardToolbar setItems:[[NSArray alloc] initWithObjects: extraSpace, next, nil]];
}
issue.inputAccessoryView = keyboardToolbar;
datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
issue.inputView = datePicker;
在你的switchToNextField方法中,只需使用[nextTextField becomeFirstResponder]将nextField作为firstResponder;
答案 2 :(得分:0)
您可以创建一个视图,然后将任何UI元素放在其中。
@property (nonatomic,strong) UIPopoverController *popoverControllerBirthday;
将以上内容添加到.h文件并在.m @synthesize popoverControllerBirthday;
还会将您的@synthesize datePicker;
更改为@synthesize datePicker=_datePicker;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField==self.yourtextfield) {
UIViewController* popoverContent = [[UIViewController alloc] init]; //ViewController
UIView *popoverView = [[UIView alloc] init]; //view
popoverView.backgroundColor = [UIColor blackColor];
_datePicker=[[UIDatePicker alloc]init];//Date picker
_datePicker.frame=CGRectMake(0,0,320, 216);
_datePicker.datePickerMode = UIDatePickerModeDate;
[_datePicker setMinuteInterval:5];
[_datePicker setTag:10];
[popoverView addSubview:_datePicker];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(result)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Next" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 220, 320, 45);
UIColor *titleColor = [UIColor whiteColor];
[button setTitleColor:titleColor forState:UIControlStateNormal];
// UIImage *doneButtonImage = [UIImage imageNamed:@"create event button.png"]; choose a button background if you ahve one
[button setBackgroundImage:doneButtonImage forState:UIControlStateNormal];
[popoverView addSubview:button];
popoverContent.view = popoverView;
popoverControllerBirthday = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
popoverControllerBirthday.delegate=self;
CGRect myFrame =textField.frame;
myFrame.origin.x = 200;// your coordinates change it as you wish
myFrame.origin.y = 195;
[popoverControllerBirthday setPopoverContentSize:CGSizeMake(320, 265) animated:NO];
[popoverControllerBirthday presentPopoverFromRect:myFrame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
return NO; // tells the textfield not to start its own editing process (ie show the keyboard)
}
else{
return YES;
}
}
//handle your result call next uitextfield in your case
-(void) result
{
//in result method you can get date using datepicker.date
if (self.popoverControllerBirthday)
{
[self.popoverControllerBirthday dismissPopoverAnimated:NO];
self.popoverControllerBirthday = nil;
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:@"M d yyyy"];
NSString *parseDatePickerDAte =[dateFormatter stringFromDate:_datePicker.date]; // your string that is choosen at datepicker
// self.textfield.text = parseDatePickerDAte ; set this to your textfield if you want
//google this dont remember exact code for that, just changing your textfield to desired one
[self.nexttextfield becomesFirstResponder];
}
答案 3 :(得分:0)
您可以使用SMDatePicker代替UIDatePicker。它将使用UIToolbar和UIDatePicker创建自定义视图。您可以使用必要的按钮轻松自定义工具栏看看示例项目。