1
votes

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.

1
So you are essentially trying to have a UITextFieldDelegate method retrieve the string from your UITextField? Also, what do you mean by UIView void methods?davetw12
@ davetw12, not quite so. I can't use received string at the right time. First, i need to receive a String, then use it to select the row. But now it works backwards. I'm trying to use different methods to receive string and setRow:inComponent. The void methods didMoveToSuperview, willMoveToWindow and etc. Can't find right way.Woof
To make sure that I am following you correctly: the first time that you set the value in your textField with your picker, everything works exactly as you'd like it to?davetw12
Let me give an example. The TextField is empty. When I begin editing it, the picker did appear with a value - January 2013 by default. Then I select in the picker March 2015, the textField shows that value. Everything is fine. Then I tap the button in an inputAccessoryView to hide a picker. The picker did disappear. Then I tap on the textField the second time and the picker did appear again with January 2013, but I need the picker to appear with March 2015. The delegate from the picker to the TextField works correct, but the delegate from the textField to the Picker works incorrectlyWoof
Are you saving the values for the date after it is set in your picker view?davetw12

1 Answers

0
votes

The reason the value of your picker resets after closing is because you aren't declaring it globally. You accomplish this by adding the following to BasicInfoViewController.m. Now all values stored in datePicker will be saved.

@interface BasicInfoViewController
{ 
    YBCDatePicker *datePicker;
}

- (void)viewDidLoad
{
    datePicker = [[YBCDatePicker alloc] init]; 
}