【问题标题】:UIPickerView select row doesn't work ios7UIPickerView 选择行不起作用 ios7
【发布时间】:2014-03-11 16:38:21
【问题描述】:

我有一个 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];

}

【问题讨论】:

  • 你能显示设置i的代码吗
  • 完成我编辑了我的代码
  • 你是否设置了一个断点来查看 isEqual 是否发生过?
  • 只是好奇:为什么要添加到 self.view 然后删除?
  • 使用您的所有代码构建一个模拟,如图所示,进入 subVC 并返回,进入父级并返回(重新创建选择器)没有问题,并且将选择器设置为一行都可以正常工作。 (顺便说一句,你的第一句话不正确,不是吗?,你不是在推动pickerview,你是在推动一个视图控制器,其中包含一个以pickview 作为输入视图的textview。)你是否用NSLog(@"picker was(is): %d",[_countryPicker selectedRowInComponent:0]); 包围了setComponent绝对确定它正在设置/检索 selectedRow

标签: ios iphone objective-c uinavigationcontroller uipickerview


【解决方案1】:

您写道:“其中 i 是动态的(根据该视图控制器中正在更改的数据进行更改)”” 在您的代码中,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如果你检查它你会发现一切正常。

【讨论】:

  • 我创建了一个项目github.com/Avtolic/SOHelper 来检查问题可能出在哪里。在我的项目中,一切正常。也许它会帮助你找到你的问题。
【解决方案2】:

设置选定的行后,您可以调用它...

[_countryPicker reloadAllComponents];

那会抹杀你的选择吗?我会删除那行

【讨论】:

    【解决方案3】:

    如果您将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;
    }
    

    【讨论】:

    • 嗨,我在 NSLog 中看到所选国家/地区代码设置为用户选择的正确国家/地区代码,但在 uipickerview 中,尽管代码被调用,但并未选择该行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多