【问题标题】:How to show selected value of a UIPickerView according to label如何根据标签显示 UIPickerView 的选定值
【发布时间】:2014-01-14 13:49:21
【问题描述】:

我正在尝试根据视图中标签的值显示具有选定值的 UIPickerView。

例如,如果标签值为 2,当我打开 pickerView 时,我希望在选取器中选择值 2。这将如何发生?

我的代码:

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

  //res is array
  return [[res objectAtIndex:row]objectForKey:@"choice_name"];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    //choiceList is sting
    self.choiceList = [NSString stringWithFormat:@"<r_PM act='closepos_choice' cl_choice='%d'/>", row];
}

 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {

UILabel *pickerLabel = (UILabel *)view;
if (pickerLabel == nil) {
    CGRect frame = CGRectMake(0.0, 0.0, 300, 30);
    pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
    [pickerLabel setTextAlignment:UITextAlignmentCenter];
    [pickerLabel setBackgroundColor:[UIColor clearColor]];
    [pickerLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:16.0]];

    //EDITED CODE FOR C_X
    NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
    [f setNumberStyle:NSNumberFormatterDecimalStyle];
    NSNumber * myNumber = [f numberFromString:lblOtomatikPozKap.text];
    [f release];

    NSInteger index = [res indexOfObject:myNumber];

    if(index != NSNotFound ){

        [self.pickerView selectRow:index inComponent:0 animated:NO];
    }

    //EDITED FOR AUSTIN
    NSInteger indexOfItem = [res indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    return [[obj objectForKey:@"choice_name"] isEqualToString:@"2"];
    }];

    if (indexOfItem != NSNotFound)
    {
    [self.pickerView selectRow:indexOfItem inComponent:1 animated:NO];
    }
 }

NSString *str = [NSString stringWithFormat:@"%d- %@", row, [[res objectAtIndex:row]objectForKey:@"choice_name"]];

[pickerLabel setText:str];

return pickerLabel;
}

//Not working at all
- (void)selectRow:(UIPickerView*)row inComponent:(NSInteger)component animated:(BOOL)animated
{}

【问题讨论】:

    标签: ios uipickerview uipickerviewcontroller


    【解决方案1】:

    您可以通过在pickerview selectRow:inComponent:animated 上调用此方法来选择一行: 编辑:

    您正在从res 获取字符串,首先您必须将标签字符串添加到此数组中,以便您可以在pickerView 中拥有这一行。 之后在 viewWillAppear 或您想要调用此方法的任何其他方法中。 首先获取该 sring/object 的索引。

    NSInteger index=[resindexOfObject:label.text];
    

    现在选择行。

    if(index != NSNotFound ){
     [self.myPicker selectRow:index inComponent:0 animated:NO]
    }
    

    【讨论】:

    • 嗨 C_X,你能告诉我更多细节吗?谢谢。
    • 我已经测试了上面的代码它不起作用。可以帮忙吗?
    • 你是如何测试这个的,你是否在数组中添加标签文本,你是否把你的代码放在正确的地方?如果你显示你的代码会更好......
    • 为什么要添加这个方法,这是pickerView的实例方法,使用它,从viewDidAppear或者ViewWillAppear或者picker视图加载数据之后调用那个方法
    • 谢谢 C_X。我已经尝试了 viewDidAppear 或 ViewWillAppear 并在方法内部传递,但仍然选择器显示第一行。
    【解决方案2】:

    首先,您将获得要从数据源(在本例中为 res 数组)中选择的值的索引。然后,在显示选择器之前调用selectRow:inComponent:animated:

    NSInteger indexOfItem = [res indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        return [[obj objectForKey:@"choice_name"] isEqualToString:@"2"];
    }];
    
    if (indexOfItem != NSNotFound)
    {
        [self.pickerView selectRow:indexOfItem inComponent:1 animated:NO];
    }
    
    // Show picker here
    

    【讨论】:

    • 嗨,奥斯汀,根据您的代码,有没有其他方法可以代替在下面显示选择器?谢谢。
    • 您不必向选择器显示// Show picker here 注释所在的位置;那只是一个建议。您可以在创建选择器后的任何时候执行此操作,即使在您显示它之后也是如此。
    • 很抱歉它不起作用。你能帮帮我吗?
    • if (indexOfItem != NSNotFound) 上设置断点并检查索引是什么。确保res 也不是nil 或类似的东西。
    • 它没有在indexOfItemIF内部传递
    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    相关资源
    最近更新 更多