如何设置一个UIPickerView的默认值
我的UIPickerView有问题。 我有3个值欧盟AP和NA。 当我启动应用程序欧盟似乎被选中,但当我做一个NSLog(@"%@", [regions objectAtIndex:row]);
我只返回(null)
,现在当我触摸UIPickerView EU值被选中,我得到一个NSLog的"EU"
。
我的问题是:
当用户只启动应用程序而什么也不触摸时,如何定义select的默认值(不仅是标签)。
编辑:这是我的代码来获取选定的项目:
#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); }
TL:DR版本:
//Objective-C [self.picker selectRow:2 inComponent:0 animated:YES]; //Swift picker.selectRow(2, inComponent:0, animated:true)
要么你没有设置你的select器来select行(你说你似乎做了,但无论如何):
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
或者您没有使用以下方法从您的选取器中获取选定的项目
- (NSInteger)selectedRowInComponent:(NSInteger)component
这将从您的选取器中将选定的行作为Integer,并按照您的要求进行操作。 这应该做的伎俩耶。 祝你好运。
无论如何阅读ref: 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解决scheme的编辑(来源: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显示以下内容:
这是如何设置一个UIPickerView的默认值
[self.picker selectRow:4 inComponent:0 animated:YES];
Swift解决scheme:
定义一个出口:
@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显示以下内容:
你必须发送- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
到select器视图出现之前。 该文档指出,方法selectedRowInComp …将给-1,因此可能是select器视图处于没有选定行的状态。 原来在创build时处于这种状态。
我也有这个问题。 但显然有一个方法调用顺序的问题。 您必须致电:
[self.picker selectRow:2 inComponent:0 animated:YES];
打完电话后
[self.view addSubview:self.picker];
在正常情况下,你可以在viewDidLoad方法中做这样的事情;
[_picker selectRow:1 inComponent:0 animated:YES];
在我的情况下,我想从API服务器获取数据,并将其显示到UIPickerView
然后我希望select器默认select第first
项目。
UIPickerView 看起来就像它在创build后select了第一个项目,但是当您尝试使用selectedRowInComponent
获取选定的索引时,您将得到NSNull
。 这是因为它没有检测到用户改变 (从0中select0)。
以下是我的解决scheme(在viewWillAppear,我提取数据后)
[_picker selectRow:1 inComponent:0 animated:NO]; [_picker selectRow:0 inComponent:0 animated:NO];
它有点脏,但不要担心,iOS中的UI渲染非常快;)
For example: you populated your UIPickerView with array values, then you wanted
在pickerView的第一个加载中select一个特定的数组值,比如“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];