UIPickerView选择行不起作用ios7

时间:2014-03-11 16:38:21

标签: ios iphone objective-c uinavigationcontroller uipickerview

我有一个UIPickerView正在推动"像这样的UINavigationController:

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

我想设置选定的行。

我在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];

其中i是动态的(根据在该视图控制器中更改的数据进行更改)

仅当我重新启动应用程序时才有效。

如果我在导航中来回走动它就不起作用

如何让UIPickerView选择正确的行?

我可以在调试模式中看到viewDidAppear中的行被调用。也许组件正在创建,我无法改变它?

这是我创建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];

}

3 个答案:

答案 0 :(得分:1)

设置选定的行后,再调用...

[_countryPicker reloadAllComponents];

这会消灭你的选择吗?我会删除该行

答案 1 :(得分:1)

如果你将单个类的selectedCountryCode变量部分(例如AppDelegate或者其他部分),然后等于该值,这肯定会起作用。在这里,我不明白即使在弹出视图后如何保留selectedCountryCode

我尝试将字符串作为AppDelegate的一部分(这当然不是一个好习惯。一个应该把它放在另一个单例类中)。

#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;
}

答案 2 :(得分:1)

您写道:“我在哪里是动态的(根据在该视图控制器中更改的数据进行更改)”“ 在您的代码i中是一个局部变量。你的意思是selectedCountryCode吗?

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];

我很确定selectedCountryCode未正确更新。添加NSLog(selectedCountryCode);进行检查。

更新:

似乎问题出现在您没有在问题中发布的代码中。要检查我创建并共享项目的代码。请在此处找到https://github.com/Avtolic/SOHelper如果您将检查它,您会发现一切正常。