XCode:如何直接用内容填充代码中的 UIPickerView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7330991/
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
XCode: How to fill UIPickerView in code directly with content?
提问by Martin Huwa
How do I do that? i want to fill it with values like EURO, USD, POUND and so on and paste the value into a textfield when i tap on the corresponding row.
我怎么做?我想用欧元、美元、英镑等值填充它,并在我点击相应行时将该值粘贴到文本字段中。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Make a new view, or do what you want here
UIPickerView *picker = [[UIPickerView alloc]
initWithFrame:CGRectMake(0, 244, 320, 270)];
[self.view addSubview:picker];
return NO;
}
回答by Anuj Kumar Rai
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
myTextField.text= [PickerArray objectAtIndex:row];
}
回答by Nekto
You should implement in your delegate method -(NSString*) pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
that will return titles @"EURO"
, @"USD"
, @"POUND"
, @"RUB"
for your rows.
您应该在您的委托方法-(NSString*) pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
中实现,该方法将为您的行返回 titles @"EURO"
, @"USD"
, @"POUND"
, @"RUB"
。
For example,
例如,
-(NSString*) pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
switch(component)
{
case 0:
return @"EURO";
case 1:
return @"USD";
case 2:
return @"POUND";
case 3:
return @"RUB";
}
return @"";
}
回答by Tendulkar
write code in your delegate method
在您的委托方法中编写代码
-(NSString*) pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{ return string; }
{ 返回字符串;}
In Array add like this @"one", @"two", @"three", @"four"
在数组中添加这样的@"one"、@"two"、@"three"、@"four"
回答by Ern? Simonyi
As others have already written you have to add the above mentiond delegate method to fill up your PickerView with items from an array. For writing the value into a textfield, you can do this in 2 ways. You can either do write a pickerView didSelectRow: delegate method like this:
正如其他人已经写的那样,您必须添加上面提到的委托方法来用数组中的项目填充 PickerView。要将值写入文本字段,您可以通过两种方式执行此操作。您可以编写一个 pickerView didSelectRow: 委托方法,如下所示:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
myTextField.text= [myArray objectAtIndex:row];
}
This will update the textField each time you tap on a row. The other way is that you pull the value of the pickerView on a button press or on some other action. Similar to
这将在您每次点击一行时更新 textField。另一种方法是在按下按钮或其他一些操作时拉取 pickerView 的值。相似
- (void)someAction:(id)sender
{
NSInteger row = [myPicker selectedRowInComponent:0];
myTextField.text = [myArray objectAtIndex:row];
}