简体   繁体   English

UIPickerView选择行不起作用ios7

[英]UIPickerView select row doesn't work ios7

I have a UIPickerView that is being "pushed" to UINavigationController like this: 我有一个UIPickerView被“推送”到UINavigationController,如下所示:

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

I would like to set the selected row. 我想设置选定的行。

I added in ViewDidAppear: 我在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? 如何让UIPickerView选择正确的行?

I can see in debug mode that the lines in viewDidAppear are called. 我可以在调试模式中看到viewDidAppear中的行被调用。 Maybe the component is being created and I can't change it? 也许组件正在创建,我无法改变它?

This is how I create the UIPickerView: 这就是我创建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];

}

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. 如果你将selectedCountryCode变量作为单例类的一部分(例如AppDelegate或者更好的是其他的一部分),然后等于该值,这肯定会起作用。 Here I don't understand how the selectedCountryCode is expected to be retained even after the view is popped. 在这里,我不明白即使在弹出视图后如何保留selectedCountryCode

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). 我尝试将字符串作为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;
}

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? 您写道:“我在哪里是动态的(根据在该视图控制器中更改的数据进行更改)”“在您的代码中, 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];

I am pretty sure selectedCountryCode is not updated correctly. 我很确定selectedCountryCode没有正确更新。 Add NSLog(selectedCountryCode); 添加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. 请在这里找到它https://github.com/Avtolic/SOHelper如果你检查它,你会发现一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM