Ios 将UIButton添加到UIDatePicker
我有一个UITextField,我将inputView设置为UIDatePicker,这样当按下UITextField而不是键盘时,会弹出一个UIDatePicker。我还想添加一个带有“下一步”按钮的工具栏,以便用户可以轻松跳转到下一个字段,但我不确定如何执行此操作,因为我已经在修改UITextField上的视图以获取UIDatePicker。以下是加载视图时使用的代码:Ios 将UIButton添加到UIDatePicker,ios,uitextfield,uitoolbar,uidatepicker,Ios,Uitextfield,Uitoolbar,Uidatepicker,我有一个UITextField,我将inputView设置为UIDatePicker,这样当按下UITextField而不是键盘时,会弹出一个UIDatePicker。我还想添加一个带有“下一步”按钮的工具栏,以便用户可以轻松跳转到下一个字段,但我不确定如何执行此操作,因为我已经在修改UITextField上的视图以获取UIDatePicker。以下是加载视图时使用的代码: - (void)viewDidLoad { self.datePicker = [[UIDatePicker alloc]
- (void)viewDidLoad
{
self.datePicker = [[UIDatePicker alloc] init];
self.datePicker.datePickerMode = UIDatePickerModeDate;
[self.datePicker addTarget:self action:@selector(datePickerValueChanged) forControlEvents:UIControlEventValueChanged];
issue.inputView = self.datePicker;
}
将“下一步”按钮添加到其顶部工具栏的建议?创建一个视图,用作文本字段的inputAccessoryView。此视图应包含您的按钮(例如下一步、取消、上一步)。以下是代码:
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设置为第一响应者 您可以创建一个视图,然后将任何UI元素放入其中
@property (nonatomic,strong) UIPopoverController *popoverControllerBirthday;
将以上内容添加到您的.h文件中,并在.m@overcontrollerbirthday同步它代码>
同时更改您的@syntheticdatepicker
到@synthesis-datePicker=\u-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];
}
您可以使用而不是UIDatePicker。它将使用UIToolbar和UIDatePicker创建自定义视图。您可以使用必要的按钮轻松定制工具栏。看看示例项目。您可以签出这个答案,它有在UIDatePicker上附加工具栏的代码,您可以根据需要进一步定制好的,我添加了UIToolbar*pickerToolbar=[[UIToolbar alloc]initWithFrame:cDirectMake(0,0,320,22)];pickerToolbar.barStyle=UIBarStyleBlack不透明;[pickerToolbar sizeToFit];NSMUTABLEARRY*barItems=[[NSMUTABLEARRY alloc]init];UIBarButtonItem*flexSpace=[[UIBarButtonItem alloc]initWithTitle:@“下一个”样式:UIBarbuttonItemStyleBorded目标:自我操作:@selector(expirenext)];[barItems addObject:flexSpace];[pickerToolbar设置项:barItems动画:是];expire.inputAccessoryView=选择器工具栏代码>成功了!我该如何将按钮向右对齐?这太难理解了。用代码更新您的问题。如果您对新代码有新问题,您应该提出新问题。