objective-c - 从 UIPickerView 获取值

标签 objective-c ios uipickerview

我无法从 UIPickerView 获取初始值。

这是一些代码:

.......
#define kMaximumPlayers 15
......
   - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) 
        {
            self.totalPlayersPossible = [NSMutableArray array];

            for (int x = 2; x < kMaximumPlayers; x++) 
            {
                [_totalPlayersPossible addObject:[NSNumber numberWithInt:x]];
            }
        }
        return self;
    }


    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        return [NSString stringWithFormat:@"%@", [self.totalPlayersPossible objectAtIndex:row]];


    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self.pickverView selectRow:0 inComponent:0 animated:YES];
    }
 }

当我运行应用程序时,UIPickerView 的第一行被选中。问题是我无法获取该行的值:

   - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
//here I get the value of selected row

       [self setNumberOfSelectedPlayers:[[self.totalPlayersPossible objectAtIndex:row]intValue]];
    }

The value of setNumberOfSelectedPlayers is 0.

我在这里想念什么?

最佳答案

正如 Novarg 所说,didSelectRow: 消息不会在加载时调用。不过,您可以直接调用 pickerView: titleForRow: inComponent 消息,以随时获取当前所选项目的标题。假设您想要的标题位于选择器 View 的第一个“卷轴”(组件)中:

NSString *initialTitle = [self pickerView:self.pickerView
                              titleForRow:[self pickerView selectedRowInComponent:0]
                             forComponent:0 
];

当然,这假定 self 作为 pickerView 的 UIPickerViewDelegate。

关于objective-c - 从 UIPickerView 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9590194/

相关文章:

iphone - 我们可以从App Store for iPhone获取应用程序的版本号吗?

iOS : How to convert a video PHAsset to NSData

ios - Swift:UIpickerView 在 UIlabel 上返回数字

ios - 如何在 UIPickerView 的每个组件上方设置标题?

ios - UIPickerView 用于具有不同数组的每个文本字段(Swift/Firebase)

objective-c - 为 iPhone (iOS 3.0+) 编译/使用 libjpeg?

ios - 如何让 UIKit 为我提供自定义转换的转换协调器?

iphone - 如何判断一个 View 是否是第一次加载?

ios - AWS SNS 错误参数无效 : Token Reason: Endpoint xxx already exists with the same Token, 但属性不同

ios - RestKit 中发送两个请求然后仅在两个请求返回后才执行操作的最佳方法是什么?