我有一个带有TextField和UIView的UIViewController,它带有一个带UIPickerView的xib文件。
TextField有一个UIView作为inputView。
当我开始编辑空TextView时,选择器会选择两个部分中的第一行。选择一些行后,TextField从Picker获取数据。一切都好。
但是如果TextField已经有一个来自Picker的值,并且我又开始编辑它,则选择器会再次选择第一行。我需要将Picker滚动到行,它包含与TextField相同的值。我该怎么办?
我正在尝试从TextField委派字符串值。
BasicInfoViewController.m
- (IBAction)startDateBegin:(UITextField *)sender
{
YBCDatePicker *datePicker = [[YBCDatePicker alloc]init];
datePicker.datePickerToBasicDelegate = self;
self.startDateTextField.inputView = datePicker;
datePicker.delegatedPickerDate = self.startDateTextField.text;
}
DatePicker.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:CGRectMake(0, 0, 320, 216)];
if (self) {
UIView *pickerView = [[[NSBundle mainBundle]loadNibNamed:@"DatePickerView" owner:self options:nil]lastObject];
[self addSubview:pickerView];
NSLog(@"%@",delegatedPickerDate);
}
return self;
}
NSlog返回nil。
- (void)willMoveToWindow:(UIWindow *)newWindow
{
NSLog(@"%@",delegatedPickerDate);
NSArray *tmpArray = [delegatedPickerDate componentsSeparatedByString:@" "];
if (tmpArray.count == 2) {
[self creatingMonths];
[self creatingYears];
monthLocation = [months indexOfObject:[tmpArray objectAtIndex:0]];
yearLocation = [years indexOfObject:[tmpArray objectAtIndex:1]];
[self.datePickerOutlet selectRow:monthLocation inComponent:0 animated:YES];
[self.datePickerOutlet selectRow:yearLocation inComponent:1 animated:YES];
}
}
NSlog返回nil,然后委托值,然后是nil。 Picker没有滚动。
但如果我这样做:
- (void)willMoveToWindow:(UIWindow *)newWindow
{
NSLog(@"%@",delegatedPickerDate);
// NSArray *tmpArray = [delegatedPickerDate componentsSeparatedByString:@" "];
//
// if (tmpArray.count == 2) {
//
[self creatingMonths];
[self creatingYears];
//
// monthLocation = [months indexOfObject:[tmpArray objectAtIndex:0]];
// yearLocation = [years indexOfObject:[tmpArray objectAtIndex:1]];
//
// }
[self.datePickerOutlet selectRow:5 inComponent:0 animated:YES];
[self.datePickerOutlet selectRow:5 inComponent:1 animated:YES];
}
Picker滚动到那些行。
当TextField值尚未委派时,Picker似乎获取数据以选择某一行,并且当值被委派时不会滚动。
我尝试使用不同的UIView void方法,但它没有用。
编辑:
DatePicker.h
@class YBCDatePicker;
@protocol datePickerDelegate <NSObject>
@required
- (void)datePicker:(YBCDatePicker*)controller datePicked:(NSString*)datePickerStringDelegate;
@end
@interface YBCDatePicker : UIView <UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak,nonatomic)id <datePickerDelegate> datePickerToBasicDelegate;
DatePicker.m
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSInteger monthRow = [pickerView selectedRowInComponent:monthComponent];
NSInteger yearRow = [pickerView selectedRowInComponent:yearComponent];
monthString = [self.months objectAtIndex:monthRow];
yearString = [self.years objectAtIndex:yearRow];
selectedPickerDate = [NSString stringWithFormat:@"%@ %@",monthString,yearString];
[self.datePickerToBasicDelegate datePicker:self datePicked:selectedPickerDate];
}
BasicInfoViewController.m
- (void)datePicker:(YBCDatePicker *)controller datePicked:(NSString *)datePickerStringDelegate
{
self.startDateTextField.text = datePickerStringDelegate;
}
P.S。 我已经采用另一种方法来创建一个选择器。我只是在一个UIViewController .m文件中分配并初始化一个没有xib文件和其他UIView类的选择器。并且所有工作都很棒,因为我不需要在不同的.m文件之间委托值。
但问题仍然需要解决,因为我想使用一个带有不同ViewControllers的选择器的UIView。
答案 0 :(得分:0)
关闭后选择器的值重置的原因是因为您没有全局声明它。您可以通过将以下内容添加到BasicInfoViewController.m来实现此目的。现在,将保存存储在datePicker中的所有值。
@interface BasicInfoViewController
{
YBCDatePicker *datePicker;
}
- (void)viewDidLoad
{
datePicker = [[YBCDatePicker alloc] init];
}