如何设置UIPickerView的默认值

时间:2012-08-02 12:06:12

标签: ios objective-c xcode swift uipickerview

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

我的问题是:

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

编辑:以下是获取所选项目的代码:

#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);


}

7 个答案:

答案 0 :(得分:89)

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

然后在 viewWillAppear viewDidLoad 中,您可以使用以下内容:

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会显示以下内容:

答案 1 :(得分:18)

这是如何设置UIPickerView的默认值

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

答案 2 :(得分:8)

Swift解决方案:

定义出口:

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

然后在 viewWillAppear viewDidLoad 中,您可以使用以下内容:

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会显示以下内容:

enter image description here

答案 3 :(得分:5)

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

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

[self.view addSubview:self.picker];

答案 4 :(得分:4)

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

答案 5 :(得分:1)

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

  

[_ picker selectRow:1 inComponent:0 animated: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渲染非常快;)

答案 6 :(得分:0)

For example: you populated your UIPickerView with array values, then you wanted 

在第一次加载pickerView时选择某个数组值,例如&#34; Arizona&#34;。 请注意,&#34;亚利桑那州&#34;是在索引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];