66

我的 UIPickerView 有问题。我在其中有 3 个值 EU AP 和 NA。当我启动应用程序时,似乎选择了 EU,但是当我创建一个时,NSLog(@"%@", [regions objectAtIndex:row]);我只返回(null),现在当我触摸 UIPickerView 时,EU 值被选中,我"EU"从 NSLog 中返回。

我的问题是:

当用户只启动应用程序并且什么都不碰时,我如何定义一个被选中的默认值(不仅是标签)。

编辑:这是我获取所选项目的代码:

#pragma mark -
#pragma mark PickerView DataSource

- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
    return [regions count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component
{
    return [regions objectAtIndex:row];
}

#pragma mark -
#pragma mark PickerView Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component
{

                selectedRegion = [[NSString alloc] initWithFormat:
                              @"%@", [regions objectAtIndex:row]];
    NSLog(@"%@", selectedRegion);


}
4

7 回答 7

107

TL:DR 版本:

//Objective-C
[self.picker selectRow:2 inComponent:0 animated:YES];
//Swift
picker.selectRow(2, inComponent:0, animated:true)

要么你没有设置你的选择器来选择行(你说你似乎已经这样做了,但无论如何):

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated

或者您没有使用以下方法从您的选择器中获取所选项目

- (NSInteger)selectedRowInComponent:(NSInteger)component

这将从您的选择器中获取选定的行作为整数,并随心所欲地使用它。这应该可以解决问题。祝你好运。

无论如何阅读参考: https ://developer.apple.com/documentation/uikit/uipickerview


编辑:

在 UIPickerView 中手动设置和获取选定行的示例:

.h 文件:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
{
    UIPickerView *picker;
    NSMutableArray *source;
}

@property (nonatomic,retain) UIPickerView *picker;
@property (nonatomic,retain) NSMutableArray *source;

-(void)pressed;

@end

.m 文件:

#import "ViewController.h"

@interface ViewController ()

@end
@implementation ViewController

@synthesize picker;
@synthesize source;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.view.backgroundColor = [UIColor yellowColor];

    self.source = [[NSMutableArray alloc] initWithObjects:@"EU", @"USA", @"ASIA", nil];

    UIButton *pressme = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 280, 80)];
    [pressme setTitle:@"Press me!!!" forState:UIControlStateNormal];
    pressme.backgroundColor = [UIColor lightGrayColor];
    [pressme addTarget:self action:@selector(pressed) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:pressme];

    self.picker = [[UIPickerView alloc] initWithFrame:CGRectMake(20, 110, 280, 300)];
    self.picker.delegate = self;
    self.picker.dataSource = self;
    [self.view addSubview:self.picker];

    //This is how you manually SET(!!) a selection!
    [self.picker selectRow:2 inComponent:0 animated:YES];
}

//logs the current selection of the picker manually
-(void)pressed
{
    //This is how you manually GET(!!) a selection
    int row = [self.picker selectedRowInComponent:0];

    NSLog(@"%@", [source objectAtIndex:row]);
}

- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
    return [source count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component
{
    return [source objectAtIndex:row];
}

#pragma mark -
#pragma mark PickerView Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component
{
//    NSLog(@"%@", [source objectAtIndex:row]);
}

@end

编辑 Swift 解决方案(来源:Dan Beaulieu 的回答)

定义一个出口:

@IBOutlet weak var pickerView: UIPickerView!  // for example

然后在您的viewWillAppearviewDidLoad中,例如,您可以使用以下内容:

pickerView.selectRow(rowMin, inComponent: 0, animated: true)
pickerView.selectRow(rowSec, inComponent: 1, animated: true)

如果您检查 Swift 2.0 框架,您会看到 .selectRow 定义为:

func selectRow(row: Int, inComponent component: Int, animated: Bool) 

在 Xcode 中单击.selectRow 的选项会显示以下内容:

于 2012-08-02T12:17:34.160 回答
18

这是如何设置 UIPickerView 的默认值

[self.picker selectRow:4 inComponent:0 animated:YES];
于 2014-02-07T06:10:38.063 回答
11

快速解决方案:

定义一个出口:

@IBOutlet weak var pickerView: UIPickerView!  // for example

然后在您的viewWillAppearviewDidLoad中,例如,您可以使用以下内容:

pickerView.selectRow(rowMin, inComponent: 0, animated: true)
pickerView.selectRow(rowSec, inComponent: 1, animated: true)

如果您检查 Swift 2.0 框架,您会看到.selectRow定义为:

func selectRow(row: Int, inComponent component: Int, animated: Bool) 

在 Xcode 中单击.selectRow 的选项会显示以下内容:

在此处输入图像描述

于 2015-07-02T18:40:53.073 回答
6

我也有这个问题。但显然存在方法调用顺序的问题。您必须致电:

[self.picker selectRow:2 inComponent:0 animated:YES];

打电话

[self.view addSubview:self.picker];
于 2015-04-26T00:21:53.767 回答
4

您必须在它出现之前发送 - (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated 到选择器视图。文档指出方法 selectedRowInComp... 将给出 -1,因此选择器视图可能处于没有选定行的状态。事实证明,它在创建时处于该状态。

于 2012-08-02T12:34:18.283 回答
1

在正常情况下,您可以在viewDidLoad方法中执行类似的操作;

[_picker selectRow:1 inComponent:0 动画:YES];

就我而言,我想从 api 服务器获取数据并将它们显示到UIPickerView然后我希望选择器first默认选择该项目。

UIPickerView看起来像是在创建后选择了第一个项目,但是当您尝试使用 获取所选索引时selectedRowInComponent,您将获得NSNull. 那是因为它检测到用户没有进行任何更改(从 0 中选择 0)。

以下是我的解决方案(在 viewWillAppear 中,在我获取数据后)

[_picker selectRow:1 inComponent:0 animated:NO];
[_picker selectRow:0 inComponent:0 animated:NO];

它有点脏,但不用担心,iOS 中的 UI 渲染非常快;)

于 2014-11-21T09:45:43.607 回答
1
For example: you populated your UIPickerView with array values, then you wanted 

在第一次加载 pickerView 时选择某个数组值,例如“Arizona”。请注意,“Arizona”这个词在索引 2 处。这是如何做到的 :) 享受编码。

NSArray *countryArray =[NSArray arrayWithObjects:@"Alabama",@"Alaska",@"Arizona",@"Arkansas", nil];
UIPickerView *countryPicker=[[UIPickerView alloc]initWithFrame:self.view.bounds];
countryPicker.delegate=self;
countryPicker.dataSource=self;
[countryPicker selectRow:2 inComponent:0 animated:YES];
[self.view addSubview:countryPicker];
于 2015-12-19T02:51:20.530 回答