문제

I have a UIPickerView that is being "pushed" to UINavigationController like this:

 [self.navigationController pushViewController:vc animated:YES];

I would like to set the selected row.

I added in ViewDidAppear:

for (int i = 0; i < [countryCodes count]; i++)
    {
        if ([[countryCodes objectAtIndex:i] isEqualToString:selectedCountryCode]){
            [_countryPicker selectRow:i inComponent:0 animated:YES];
            countrySelectedRow = i;
            break;
        }

    }
    [_countryPicker reloadAllComponents];

where i is dynamic (being changed based on data that is changing in that view controller)

It works only if I restart the app.

If I go back and forth in the navigation it doesn't work

How can I make the UIPickerView choose the the correct row?

I can see in debug mode that the lines in viewDidAppear are called. Maybe the component is being created and I can't change it?

This is how I create the UIPickerView:

- (void)viewDidLoad
{
_countryPicker = [[UIPickerView alloc] init];
    [self initPicker:_countryPicker textField:_countryText];
}

- (void)initPicker:(UIPickerView*)pickerView textField:(UITextField*) textField
{
    CGRect pickerFrame = CGRectMake(0, 0, 200, 216);
    pickerView.frame = pickerFrame;
    pickerView.userInteractionEnabled = YES;
    pickerView.dataSource = self;
    pickerView.hidden = YES;
    pickerView.delegate = self;
    pickerView.showsSelectionIndicator = YES;
    [self.view addSubview:pickerView];
    [textField setInputView:pickerView];

    textField.delegate = self;
    [pickerView removeFromSuperview];

}
도움이 되었습니까?

해결책

You wrote: "where i is dynamic (being changed based on data that is changing in that view controller)"" In your code i is a local variable. Did you mean selectedCountryCode here?

for (int i = 0; i < [countryCodes count]; i++)
{
    if ([[countryCodes objectAtIndex:i] isEqualToString:selectedCountryCode]){
        [_countryPicker selectRow:i inComponent:0 animated:YES];
        countrySelectedRow = i;
        break;
    }
}
[_countryPicker reloadAllComponents];

I am pretty sure selectedCountryCode is not updated correctly. Add NSLog(selectedCountryCode); to check it.

UPDATE:

It seems that problem is somewhere inside a code you did not post in the question. To check your code I created and share a project. Please find it here https://github.com/Avtolic/SOHelper If you will check it you will find that everything works ok.

다른 팁

After you set the selected row you then call this...

[_countryPicker reloadAllComponents];

Thats going to wipe out your selection? I would remove that line

If you make the selectedCountryCode variable part of a singleton class (for example AppDelegate or preferebly some other), and then equate the value, this is surely going to work. Here I don't understand how the selectedCountryCode is expected to be retained even after the view is popped.

I tried with making the string a part of AppDelegate (which is of course not a good practice. One should put it in another singleton class).

#import "CViewController.h"
#import "AppDelegate.h"


@interface CViewController ()<UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource>

@property(nonatomic, strong) UIPickerView *countryPicker;
@property (nonatomic, weak) IBOutlet UITextField *countryText;
@property (nonatomic, strong) NSArray *countryCodes;
@property (nonatomic, assign) int countrySelectedRow;
@property (nonatomic,strong) AppDelegate *delegate;

@end

@implementation CViewController


- (void)viewDidLoad
{
    self.delegate = [[UIApplication sharedApplication] delegate];
    _countryPicker = [[UIPickerView alloc] init];
    [self initPicker:_countryPicker textField:_countryText];
    self.countryCodes = @[@"A", @"B", @"C", @"D", @"E"];
}

- (void)initPicker:(UIPickerView*)pickerView textField:(UITextField*) textField
{
    CGRect pickerFrame = CGRectMake(0, 0, 200, 216);
    pickerView.frame = pickerFrame;
    pickerView.userInteractionEnabled = YES;
    pickerView.dataSource = self;
    pickerView.hidden = YES;
    pickerView.delegate = self;
    pickerView.showsSelectionIndicator = YES;
    [self.view addSubview:pickerView];
    [textField setInputView:pickerView];

    textField.delegate = self;

    [pickerView removeFromSuperview];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [self.countryCodes objectAtIndex:row];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [self.countryCodes count];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

    self.delegate.selectedCountryCode = [self.countryCodes objectAtIndex:row];
    NSLog(@"picker was(is): %d",[_countryPicker selectedRowInComponent:0]);
}


-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    for (int i = 0; i < [self.countryCodes count]; i++)
    {
        if ([[self.countryCodes objectAtIndex:i] isEqualToString:self.delegate.selectedCountryCode]){
            [_countryPicker selectRow:i inComponent:0 animated:YES];
            self.countrySelectedRow = i;
            break;
        }

    }
    [_countryPicker reloadAllComponents];
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.countryPicker.hidden = NO;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top