自定义UIDatePicker的调整代码
所以我发现这个惊人的代码在这里弹出一个UIDatePicker。 (Matthias Bauch的荣誉) 我想用自定义选取轮来定制它,但代码处于复杂度级别,我不知道如何去切换到UIPicker以及如何用我的填充选择器阵列。自定义UIDatePicker的调整代码
这里是原代码:
- (void)changeDate:(UIDatePicker *)sender { NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"cccc, MMM d, hh:mm aa"];
NSString *formattedVersion = [dateFormat stringFromDate:sender.date];
NSLog(@"%@",formattedVersion);
}
- (void)removeViews:(id)object {
[[self.view viewWithTag:9] removeFromSuperview];
[[self.view viewWithTag:10] removeFromSuperview];
[[self.view viewWithTag:11] removeFromSuperview];
}
- (void)dismissDatePicker:(id)sender {
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height, 320, 44);
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height+44, 320, 216);
[UIView beginAnimations:@"MoveOut" context:nil];
[self.view viewWithTag:9].alpha = 0;
[self.view viewWithTag:10].frame = datePickerTargetFrame;
[self.view viewWithTag:11].frame = toolbarTargetFrame;
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(removeViews:)];
[UIView commitAnimations];
}
- (IBAction)callDP:(id)sender {
if ([self.view viewWithTag:9]) {
return;
}
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height-216-44, 320, 44);
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height-216, 320, 216);
UIView *darkView = [[[UIView alloc] initWithFrame:self.view.bounds] autorelease];
darkView.alpha = 0;
darkView.backgroundColor = [UIColor blackColor];
darkView.tag = 9;
UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDatePicker:)] autorelease];
[darkView addGestureRecognizer:tapGesture];
[self.view addSubview:darkView];
UIDatePicker *datePicker = [[[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 216)] autorelease];
datePicker.tag = 10;
[datePicker addTarget:self action:@selector(changeDate:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
UIToolbar *toolBar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)] autorelease];
toolBar.tag = 11;
toolBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *spacer = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIBarButtonItem *doneButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissDatePicker:)] autorelease];
[toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
[self.view addSubview:toolBar];
[UIView beginAnimations:@"MoveIn" context:nil];
toolBar.frame = toolbarTargetFrame;
datePicker.frame = datePickerTargetFrame;
darkView.alpha = 0.5;
[UIView commitAnimations];
}
我想补充我UIPicker与拣货机轮这些值:
Days=[[NSArray alloc] initWithObjects: @"Today", @"Tomorrow", nil]; Hours = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12", nil];
Minutes=[[NSArray alloc] initWithObjects:@"05", @"10", @"15", @"20", @"25", @"30", @"35", @"40", @"45", @"50", @"55", nil];
任何帮助/指导将非常感激!
感谢, 垫
回答:
您将需要使用UIPickerView
代替UIDatePicker
的。然后您需要提供适当的委托方法来为其提供数据。
首先创建一个数组来保存项目,将可在选择器:
NSMutableArray *arrayColors = [[NSMutableArray alloc] init]; [arrayColors addObject:@"Red"];
[arrayColors addObject:@"Orange"];
实施拾取数据源的方法:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView { return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [arrayColors count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [arrayColors objectAtIndex:row];
}
和委托方法得到什么项目的用户选择:
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { NSLog(@"Selected Color: %@. Index of selected color: %i", [arrayColors objectAtIndex:row], row);
}
这里是documentation和良好的tutorial
更新
要创建一个UIPickerView编程确保它看起来是这样的:
//Make sure our class knows we have picker delegate/data source methods in it @interface myViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
创建
//Picker creation UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];
一个方便的委托方法
// tell the picker the width of each row for a given component. no necessary but helpful if need be - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
int sectionWidth = 300;
return sectionWidth;
}
回答:
添加随机的评论。如果您想在按下UITextField将信息输入到文本字段时使用此拾取器替换键盘,请按以下步骤操作。
[<Your UI text field> setInputView:<your picker view>];
例如:
NSMutableArray *pickerData = [[NSMutableArray alloc]initWithObjects:@"",@"1", @"2", @"3", @"4", @"5", @"6", @"7", nil]; UIPickerView *myPickerView = [[UIPickerView alloc]init];
myPickerView.delegate = self;
myPickerView.dataSource = self;
myPickerView.showsSelectionIndicator = YES;
[self.myTexfield setInputView:myPickerView];
以上是 自定义UIDatePicker的调整代码 的全部内容, 来源链接: utcz.com/qa/260658.html