ios 如何设置 UIPickerView 的默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11777072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set a default Value of a UIPickerView
提问by Chris
I have a problem with my UIPickerView.
I have 3 values in it EU AP and NA.
When I start the app EU seems to be selected but when I make a NSLog(@"%@", [regions objectAtIndex:row]);
I only get back (null)
,
now when I touch the UIPickerView the EU value is selected and I get "EU"
back from a NSLog.
我的 UIPickerView 有问题。我在 EU AP 和 NA 中有 3 个值。当我启动应用程序 EU 似乎被选中但当我做一个NSLog(@"%@", [regions objectAtIndex:row]);
我只回来(null)
,现在当我触摸 UIPickerView 欧盟值被选中,我"EU"
从 NSLog 回来。
My question is:
我的问题是:
How can I define a default value which is selected (not only the label) when the user only starts the app and touches nothing.
如何定义当用户仅启动应用程序而未触摸任何内容时选择的默认值(不仅是标签)。
Edit:Here is my code to get the selected item:
编辑:这是我获取所选项目的代码:
#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);
}
回答by Totumus Maximus
TL:DR version:
TL:DR 版本:
//Objective-C
[self.picker selectRow:2 inComponent:0 animated:YES];
//Swift
picker.selectRow(2, inComponent:0, animated:true)
Either you didn't set your picker to select the row (which you say you seem to have done but anyhow):
要么你没有设置你的选择器来选择行(你说你似乎已经完成了,但无论如何):
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
OR you didn't use the the following method to get the selected item from your picker
或者您没有使用以下方法从选择器中获取所选项目
- (NSInteger)selectedRowInComponent:(NSInteger)component
This will get the selected row as Integer from your picker and do as you please with it. This should do the trick for yah. Good luck.
这将从您的选择器中获取所选行作为整数,并随心所欲地使用它。这应该可以解决问题。祝你好运。
Anyhow read the ref: https://developer.apple.com/documentation/uikit/uipickerview
无论如何阅读参考:https: //developer.apple.com/documentation/uikit/uipickerview
EDIT:
编辑:
An example of manually setting and getting of a selected row in a UIPickerView:
在 UIPickerView 中手动设置和获取选定行的示例:
the .h file:
.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
the .m file:
.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
EDIT for Swift solution (Source: Dan Beaulieu's answer)
Swift 解决方案的编辑(来源:Dan Beaulieu 的回答)
Define an Outlet:
定义一个出口:
@IBOutlet weak var pickerView: UIPickerView! // for example
Then in your viewWillAppearor your viewDidLoad, for example, you can use the following:
然后在您的viewWillAppear或您的viewDidLoad 中,例如,您可以使用以下内容:
pickerView.selectRow(rowMin, inComponent: 0, animated: true)
pickerView.selectRow(rowSec, inComponent: 1, animated: true)
If you inspect the Swift 2.0 framework you'll see .selectRow defined as:
如果您检查 Swift 2.0 框架,您会看到 .selectRow 定义为:
func selectRow(row: Int, inComponent component: Int, animated: Bool)
option clicking.selectRow in Xcode displays the following:
在 Xcode 中单击.selectRow选项显示以下内容:
回答by Hardik Darji
This is How to set a default Value of a UIPickerView
这是如何设置 UIPickerView 的默认值
[self.picker selectRow:4 inComponent:0 animated:YES];
回答by Dan Beaulieu
Swift solution:
迅捷解决方案:
Define an Outlet:
定义一个出口:
@IBOutlet weak var pickerView: UIPickerView! // for example
Then in your viewWillAppearor your viewDidLoad, for example, you can use the following:
然后在您的viewWillAppear或您的viewDidLoad 中,例如,您可以使用以下内容:
pickerView.selectRow(rowMin, inComponent: 0, animated: true)
pickerView.selectRow(rowSec, inComponent: 1, animated: true)
If you inspect the Swift 2.0 framework you'll see .selectRow
defined as:
如果您检查 Swift 2.0 框架,您将看到.selectRow
定义为:
func selectRow(row: Int, inComponent component: Int, animated: Bool)
option clicking.selectRow in Xcode displays the following:
在 Xcode 中单击.selectRow选项显示以下内容:
回答by user4833236
I too had this problem. But apparently there is an issue of the order of method calls. You must call:
我也有这个问题。但显然存在方法调用顺序的问题。您必须致电:
[self.picker selectRow:2 inComponent:0 animated:YES];
aftercalling
打电话后
[self.view addSubview:self.picker];
回答by Per Arve
You have to send
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
to the picker view before it appears. The documentation states that the method selectedRowInComp... will give -1, thus it is possible that the picker view is in a state with no selected row. It turns out to be in that state when created.
您必须- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
在它出现之前发送
到选择器视图。该文档指出, selectedRowInComp... 方法将给出 -1,因此选择器视图可能处于没有选定行的状态。事实证明,创建时处于该状态。
回答by Ryan Wu
In normal case, you can do something like this in viewDidLoadmethod;
在正常情况下,你可以在viewDidLoad方法中做这样的事情;
[_picker selectRow:1 inComponent:0 animated:YES];
[_picker selectRow:1 inComponent:0 动画:YES];
In my case, I'd like to fetch data from api server and display them onto UIPickerView
then I want the picker to select the first
item by default.
就我而言,我想从 api 服务器获取数据并将它们显示在上面,UIPickerView
然后我希望选择器first
默认选择该项目。
The UIPickerView will look likeit selected the first item after it was created, but when you try to get the selected index by using selectedRowInComponent
, you will get NSNull
.
That's because it detected nothing changedby the user (select 0 from 0 ).
UIPickerView看起来像是在创建后选择了第一项,但是当您尝试使用 获取所选索引时selectedRowInComponent
,您将获得NSNull
. 那是因为它检测到用户没有任何更改(从 0 中选择 0 )。
Following is my solution (in viewWillAppear, after I fetched the data)
以下是我的解决方案(在 viewWillAppear 中,在我获取数据之后)
[_picker selectRow:1 inComponent:0 animated:NO];
[_picker selectRow:0 inComponent:0 animated:NO];
Its a bit dirty, but dont worry, the UI rendering in iOS is very fast ;)
它有点脏,但别担心,iOS 中的 UI 渲染非常快;)
回答by handiansom
For example: you populated your UIPickerView with array values, then you wanted
to select a certain array value in the first load of pickerView like "Arizona". Note that the word "Arizona" is at index 2. This how to do it :) Enjoy coding.
在像“Arizona”这样的pickerView的第一次加载中选择某个数组值。请注意,“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];