I have one UIViewController with a TextField and a UIView, which is loaded with xib file with a UIPickerView.
The TextField has a UIView as an inputView.
When I begin editing an empty TextView, the picker selects the first row in both sections. After selecting some rows, the TextField gets data from a Picker. Everything is fine.
But if the TextField already has some value from a Picker, and I started to edit it again, the picker again selects the first row. I need to scroll a Picker to row, that contains same value as the TextField. How can I do it?
I'm trying to delegate a String Value from the 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 returns 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 returns nil, then delegated value, then nil. The Picker didn't scroll.
But if I do something like this:
- (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];
}
The Picker scrolls to that rows.
It seems that The Picker gets data to select some row when a TextField value didn't delegated yet, and didn't scroll when value did delegated.
I tried to use different UIView void methods, but it didn't work.
EDIT:
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. I've made another way to create a picker. I've just alloc and init a picker without xib files and other UIView classes in one UIViewController .m file. And all works great, because I don't need to delegate values between different .m files.
But the problem is still need to solve, because I want to use one UIView with a picker with different ViewControllers.