UIpickerView 在同一个 ScrollView 中填充多个 uitextFields

发布于 2024-11-30 10:50:35 字数 1259 浏览 3 评论 0原文

你好,我正在开发一个 iPhone 应用程序,我在其中填充了一个 uipickerview,它与一个 uitextfield 配合使用效果很好。但我需要填充 6 个 uitextfield,并且我为每个 UItextfield 使用相同的 uipickerview。

我正在使用加载了数组对象的 Ipickerview,并且当触摸每个字段时它会弹出。问题在于 UItextfields 下面的代码共享来自选择器的相同数据。

我不知道如何编码,以便每个字段从 UIPickerView 行获取自己的数据。 我做错了什么?有编码建议吗?

谢谢

@implementation BestViewController

-(NSInteger)numberOfComponentsInPickerView: (UIPickerView *)thePickerView 
{ return 1; }

-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:                    (NSInteger)component 
{return [list count]; }

-(NSString *)pickerView:(UIPickerView *)thePickerview titleForRow:(NSInteger)row  forComponent:(NSInteger)component 
{return [list objectAtIndex:row]; }

-(void)pickerView:(UIPickerView *)thePickerview didSelectRow:(NSInteger)row inComponent:      (NSInteger)component 
{ 
    uitextfield1.text = [list objectAtIndex:row];   
    utitextfield2.text = [list objectAtIndex:row];
}



- (void)viewDidLoad {

[super viewDidLoad];

    uitextfield1.inputView = pickerView; 
uitextfield2.inputView = pickerView;

list = [[NSMutableArray alloc] init];   
[list addObject:@"a"];
[list addObject:@"b"];
    [list addObject:@"c"];  
    [list addObject:@"d"];
}    

Hi I am developing an iphone app where I have populated a uipickerview and it works great with one uitextfield. But I have 6 uitextfield I need to populate and I am using the same uipickerview for each UItextfield.

I have working the Ipickerview loaded with array objects and it pops up when each field is touched. The problem is with the code below the UItextfields share the same data from the picker.

I can not figure out how to code so each field gets it own data from the UIPickerView row.
What Am I doing wrong? Any coding suggestions?

Thanks

@implementation BestViewController

-(NSInteger)numberOfComponentsInPickerView: (UIPickerView *)thePickerView 
{ return 1; }

-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:                    (NSInteger)component 
{return [list count]; }

-(NSString *)pickerView:(UIPickerView *)thePickerview titleForRow:(NSInteger)row  forComponent:(NSInteger)component 
{return [list objectAtIndex:row]; }

-(void)pickerView:(UIPickerView *)thePickerview didSelectRow:(NSInteger)row inComponent:      (NSInteger)component 
{ 
    uitextfield1.text = [list objectAtIndex:row];   
    utitextfield2.text = [list objectAtIndex:row];
}



- (void)viewDidLoad {

[super viewDidLoad];

    uitextfield1.inputView = pickerView; 
uitextfield2.inputView = pickerView;

list = [[NSMutableArray alloc] init];   
[list addObject:@"a"];
[list addObject:@"b"];
    [list addObject:@"c"];  
    [list addObject:@"d"];
}    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把时间冻结 2024-12-07 10:50:35

您需要获取当前的第一响应者并设置其 text 属性,而不是显式设置特定的文本字段。

因此,

-(void)pickerView:(UIPickerView *)thePickerview didSelectRow:(NSInteger)row inComponent:      (NSInteger)component 
{ 
    // textFields is an NSArray holding each of the textfields that are using the picker as an input view
    for (UITextField textField in textFields)
    {
        if ([textField isFirstResponder])
        {
            textField.text = [list objectAtIndex:row];
            break;
        }
    }    
}

可能有一种更优雅的方式来找到当前的第一响应者,这超出了我的想象。 textFields 可能是一个 IBOutletCollection,但我自己没有使用过它们,所以我不能有太多权威。

You need to get hold of the current first responder and set its text property rather than explicitly setting a particular text field.

So,

-(void)pickerView:(UIPickerView *)thePickerview didSelectRow:(NSInteger)row inComponent:      (NSInteger)component 
{ 
    // textFields is an NSArray holding each of the textfields that are using the picker as an input view
    for (UITextField textField in textFields)
    {
        if ([textField isFirstResponder])
        {
            textField.text = [list objectAtIndex:row];
            break;
        }
    }    
}

There may be a more elegant way to find the current first responder, this is off the top of my head. textFields could possibly be an IBOutletCollection, but I haven't used those myself so I can't speak with much authority.

扬花落满肩 2024-12-07 10:50:35

我将这样做:

首先,定义几个不同的选择器。当然,您使用相同的 UIPickerView 但您更改了一个属性来帮助您区分它们。 (几乎)每个文本字段都有不同的数据。 Apple 专门为此目的设计的一个方便的属性是 tag,它是每个 UIView 可用的任意整数。您可以将相同的标签分配给 UITextField

例如:

#define kFirstTextField 101
#define kSecondTextField 102
#define kThirdTextField 103
//... etc

在触摸文本字段时的方法中:

[myPickerView setHidden:NO];  // or however you show the picker
[myPickerView setTag:textField.tag];
[myPickerView reloadAllComponents];

在选择器的数据方法中:

-(NSString *)pickerView:(UIPickerView *)thePickerview 
            titleForRow:(NSInteger)row  
           forComponent:(NSInteger)component 
{
    if (thePickerView.tag==kFirstTextField) 
        { return [list1 count]; }
    if (thePickerView.tag==kSecondTextField) 
        { return [anotherOrTheSameList count]; }
// of course, you can also use a switch statement
    return [defaultList count];
}

titleForRow: 方法中执行类似的操作。
最后,当某个东西被选中时,再次通过标签进行区分:

-(void)   pickerView:(UIPickerView *)thePickerview 
        didSelectRow:(NSInteger)row 
         inComponent:(NSInteger)component 
{ 
    UITextField *field = (UITextField *) [thePickerview.superview viewWithTag:thePickerview.tag];   
// this assumes the UIPickerView is the subview of the same view as the UITextFields
    if (thePickerview.tag==kFirstTextField) 
        { field.text = [list1 objectAtIndex:row]; }
    if (thePickerview.tag==kSecondTextField)
        { field.text = [anotherOrTheSameList objectAtIndex:row]; }
    // etc.
    // alternatively:
    field.text = [self pickerView:thePickerview titleForRow:row forComponent:0];
}

Here is how I would do it:

First, define several different pickers. Of course you use the same UIPickerView but you change one property that helps you distinguish them. There is different data for (almost) each text field. One convenient property that Apple designed exactly for this purpose is the tag, an arbitrary integer available to every UIView. You can assign the same tags to the UITextFields.

For example:

#define kFirstTextField 101
#define kSecondTextField 102
#define kThirdTextField 103
//... etc

In the method when the text field is touched:

[myPickerView setHidden:NO];  // or however you show the picker
[myPickerView setTag:textField.tag];
[myPickerView reloadAllComponents];

In the data methods of the picker:

-(NSString *)pickerView:(UIPickerView *)thePickerview 
            titleForRow:(NSInteger)row  
           forComponent:(NSInteger)component 
{
    if (thePickerView.tag==kFirstTextField) 
        { return [list1 count]; }
    if (thePickerView.tag==kSecondTextField) 
        { return [anotherOrTheSameList count]; }
// of course, you can also use a switch statement
    return [defaultList count];
}

Do something similar in the titleForRow: method.
Finally, when something is picked, discriminate again by tag:

-(void)   pickerView:(UIPickerView *)thePickerview 
        didSelectRow:(NSInteger)row 
         inComponent:(NSInteger)component 
{ 
    UITextField *field = (UITextField *) [thePickerview.superview viewWithTag:thePickerview.tag];   
// this assumes the UIPickerView is the subview of the same view as the UITextFields
    if (thePickerview.tag==kFirstTextField) 
        { field.text = [list1 objectAtIndex:row]; }
    if (thePickerview.tag==kSecondTextField)
        { field.text = [anotherOrTheSameList objectAtIndex:row]; }
    // etc.
    // alternatively:
    field.text = [self pickerView:thePickerview titleForRow:row forComponent:0];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文