ios 我将如何设置 UIPickerView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13756591/
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 would I set up a UIPickerView?
提问by Nikita Jerschow
I am setting up a UIPickerView
to have choices like choice a, choice b, choice cand so on. I have tried to interpret the sample code from Apple but that seems very difficult to understand for a beginner like me. I also would like the choices from the picker view to take me to another page if that is possible.
我正在设置 aUIPickerView
以进行选择,例如选择 a,选择 b,选择 c等等。我试图解释来自 Apple 的示例代码,但对于像我这样的初学者来说,这似乎很难理解。如果可能的话,我还希望选择器视图中的选项将我带到另一个页面。
回答by Mohit_Jaiswal
It's obvious for every beginner that it is some what tedious to understand these things the first time.
很明显,对于每个初学者来说,第一次理解这些东西是多么乏味。
Anyways, do you know how to use UITableView
s? Do you know how to use UITableViewDelegate
and UITableViewDataSource
? If your answer is yes, then just imagine UIPickerView
s are like UITableView
s (but remember they are not UITableViewController
s).
无论如何,您知道如何使用UITableView
s 吗?你知道如何使用UITableViewDelegate
和UITableViewDataSource
吗?如果你的答案是肯定的,那么想象一下UIPickerView
s 就像UITableView
s(但记住它们不是UITableViewController
s)。
Let's say, I've a UIPickerView
:
假设,我有一个UIPickerView
:
UIPickerView *objPickerView = [UIPickerView new]; // You need to set frame or other properties and add to your view...you can either use XIB code...
1) First you need to assign the delegate
and dataSource
to the UIPickerView
either via IB or code. It depends on your implementation (So this step looks very similar to a UITableView
, doesn't it?)
1)首先你需要分配delegate
并dataSource
到UIPickerView
或者通过IB或代码。这取决于您的实现(所以这一步看起来非常类似于 a UITableView
,不是吗?)
Like this:
像这样:
objPickerView.delegate = self; // Also, can be done from IB, if you're using
objPickerView.dataSource = self;// Also, can be done from IB, if you're using
2) Next, you need to define number of sections, like this:
2)接下来,您需要定义部分的数量,如下所示:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1; // Or return whatever as you intend
}
2) Then you need to define the number of rows you need:
2)然后你需要定义你需要的行数:
- (NSInteger)pickerView:(UIPickerView *)thePickerView
numberOfRowsInComponent:(NSInteger)component {
return 3;//Or, return as suitable for you...normally we use array for dynamic
}
3) Then, define title for row (And if you have multiple section, then title for each section):
3)然后,为行定义标题(如果您有多个部分,则为每个部分定义标题):
- (NSString *)pickerView:(UIPickerView *)thePickerView
titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [NSString stringWithFormat:@"Choice-%d",row];//Or, your suitable title; like Choice-a, etc.
}
4) Next, you need to get the event when someone clicks on an element (As you want to navigate to other controller/screen):
4)接下来,您需要在有人单击某个元素时获取事件(因为您想导航到其他控制器/屏幕):
- (void)pickerView:(UIPickerView *)thePickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
//Here, like the table view you can get the each section of each row if you've multiple sections
NSLog(@"Selected Color: %@. Index of selected color: %i",
[arrayColors objectAtIndex:row], row);
//Now, if you want to navigate then;
// Say, OtherViewController is the controller, where you want to navigate:
OtherViewController *objOtherViewController = [OtherViewController new];
[self.navigationController pushViewController:objOtherViewController animated:YES];
}
That's all the implementation you need.
这就是您需要的所有实现。
回答by Gabriele Petronella
I'll briefly explain how to achieve that programmatically
我将简要解释如何以编程方式实现这一目标
- (void)viewDidLoad {
[super viewDidLoad];
UIPickerView * picker = [UIPickerView new];
picker.delegate = self;
picker.dataSource = self;
picker.showsSelectionIndicator = YES;
[self.view addSubview:picker];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 3;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString * title = nil;
switch(row) {
case 0:
title = @"a";
break;
case 1:
title = @"b";
break;
case 2:
title = @"c";
break;
}
return title;
}
Basically in the viewDidLoad
we are creating and adding a UIPickerView
to the view, telling it that our controller serves both as delegate
and dataSource
(You can do this in Interface Builder too)
基本上在viewDidLoad
我们创建并添加UIPickerView
到视图中,告诉它我们的控制器同时用作delegate
和dataSource
(您也可以在界面生成器中执行此操作)
Then we are implementing two data source methods in order to tell how many components and how many rows per components the pickerView has, respectively numberOfComponentsinPickerView:
and pickerView:numberOfRowsInComponent:
然后,我们正在实施以两个数据源的方法来告诉每个组件有多少成分,多少行pickerView先后分别numberOfComponentsinPickerView:
与pickerView:numberOfRowsInComponent:
Finally we are implementing the delegate method pickerView:titleForRow:forComponent:
that returns the content of every line.
最后,我们实现了pickerView:titleForRow:forComponent:
返回每一行内容的委托方法。
If you want to customize the behavior when a row has been selected you can implement the delegate method pickerView:didSelectRow:inComponent:
which - as the name suggests - is called every time a row is selected.
如果您想在选择一行时自定义行为,您可以实现委托方法pickerView:didSelectRow:inComponent:
- 顾名思义 - 每次选择一行时都会调用该方法。
回答by Vincent Bernier
A UIPickerView use a parten similar to the one use for a UITableView. DataSource
and Delegate
protocol.
UIPickerView 使用的 parten 类似于 UITableView 的使用。DataSource
和Delegate
协议。
The DataSource
methods are used to tell the picker how many 'columns' and how many 'rows' there are in your picker.
While the Delegate
methods are for the rest, height of rows, width of columns, views or title to display and a call back when the picker got moved.
UIPickerView
这些DataSource
方法用于告诉选择器您的选择器中有多少“列”和多少“行”。其余
的Delegate
方法是行高、列宽、要显示的视图或标题以及在选择器移动时的回调。
用户界面选择器视图