【问题标题】:UIPickerView and UITextField get value from UIPickerViewUIPickerView 和 UITextField 从 UIPickerView 获取值
【发布时间】:2012-03-26 14:28:47
【问题描述】:

好的,我不知道如何解决这个问题。

我有一个 UITextField,我根据 XML 文件的定义动态添加到 UITableView,在 XML 文件中我可以指定一个列表作为数据类型之一,然后我要做的是添加一个 UIPickerView 来显示列出我的特定 UITextField 的 inputView。

我遇到的问题是弄清楚如何从 UIPickerView 中获取所选值并将其添加到我的 UITextField 的 text 属性中

我的 UIPickerView 是一个自定义类,但我没有添加任何额外功能,我只是重写了它,以便可以将数据源作为属性传递给 UIPickerView。

这是我的 PickerViewDataSourceAndDelegate.h

#import <Foundation/Foundation.h>

@interface PickerViewDataSourceAndDelegate : UIPickerView <UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) NSMutableArray *pickerData;

@end

它是 .m 文件

#import "PickerViewDataSourceAndDelegate.h"

@implementation PickerViewDataSourceAndDelegate 

@synthesize pickerData;

-(id)init {
    if (self = [super init])
    {
        self.pickerData = [[NSMutableArray alloc] init];
    }
    return self;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {

    return 1;
}

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

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [self.pickerData objectAtIndex:row];
}

@end

这就是我将 UITextField 与 UIPickerView 添加为 inputView 的方式

    for(NodeProperty *nodeproperty in node.properties)
    {
        if(nodeproperty.flowDirection == (FlowDirection*)Output)
        {
            if([nodeproperty.typeName isEqualToString:@"System.String"] && nodeproperty.extendedType == (ExtendedType*)None && [nodeproperty.enumValues count] == 0)
            {
                ...
            }
            else if([nodeproperty.typeName isEqualToString:@"System.String"] && nodeproperty.extendedType == (ExtendedType*)MultilineText && [nodeproperty.enumValues count] == 0)
            {
                ...
            }
            else if([nodeproperty.typeName isEqualToString:@"System.Boolean"] && nodeproperty.extendedType == (ExtendedType*)None && [nodeproperty.enumValues count] == 0)
            {
                ...
            }
            else if([nodeproperty.typeName isEqualToString:@"System.Double"] && nodeproperty.extendedType == (ExtendedType*)None && [nodeproperty.enumValues count] == 0)
            {
                ...
            }
            else if([nodeproperty.typeName isEqualToString:@"System.String"] && nodeproperty.extendedType == (ExtendedType*)None && [nodeproperty.enumValues count] > 0)
            {
                UILabel *fieldLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 290, 20)];
                fieldLabel.text = nodeproperty.name;
                [rowsInSectionLabels addObject:fieldLabel];

                PickerViewDataSourceAndDelegate *pickerDataDel = [[PickerViewDataSourceAndDelegate alloc] init];

                pickerDataDel.pickerData = nodeproperty.enumValues;
                pickerDataDel.dataSource = pickerDataDel;
                pickerDataDel.delegate = pickerDataDel;

                UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(5, 25, 290, 30)];
                [textField setBorderStyle:UITextBorderStyleRoundedRect];
                textField.inputView = pickerDataDel;
                [rowsInSection addObject:textField];
            }
            else
            {
              ....

为了篇幅我省略了一些空格,如果有不清楚的地方我会很乐意解释。

每件事都完美无缺,我只想从选择器中取出选定的值

【问题讨论】:

    标签: iphone ios uitextfield uipickerview


    【解决方案1】:

    请参阅picker view delegate protocol。实现这个方法:

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    

    在这里,您可以从模型中获取适当的值并更新您的文本字段。

    要确定哪个是当前文本字段,您需要找出哪个文本字段是第一响应者。

    将所有文本字段放在 IBOutletCollection 或数组中,遍历它们并找到第一响应者。

    或者,如果您有文本字段委托,您可以在 didBeginEditing 委托方法中为当前文本字段设置一个 ivar,并在上面的选取器视图委托方法中使用它。

    【讨论】:

    • 我的问题是如何将它传递回我的文本字段,因为我可以有多个文本字段在其 inputView 中有选择器视图,所以我不能动态构建所有内容
    • 您的编辑与我最终所做的非常相似,我会接受您的回答,因为它引导我朝着最终做的正确方向前进。现在我将保持它的原样,直到我完成我的演示,然后我可以进一步优化它。谢谢
    【解决方案2】:

    我找到了我自己的解决方案,我必须创建一个委托方法来将值传递回我的 UITextField。

    首先我在 PickerViewDataSourceAndDelegate.h 中声明了一个协议

    @protocol PickerRowSelected
    -(void) selectedARowAndValueIs:(NSString*)aValue;
    @end
    

    然后我添加了一个成员:

    id <PickerRowSelected> adelegate;
    

    并添加了一个属性

    @property (nonatomic, strong) id<PickerRowSelected> adelegate;
    

    然后我添加了这个功能

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
        [adelegate selectedARowAndValueIs:[self.pickerData objectAtIndex:row]];
    }
    

    在包含我添加的文本字段的控制器中:

    pickerDataDel.adelegate = self;
    

    并实现了我的新委托方法

    -(void) selectedARowAndValueIs:(NSString *)aValue
    {
        UITextField *aview = (UITextField*)[self.view findViewThatIsFirstResponder];
        aview.text = aValue;
    }
    

    查看我对这个问题的回答以找到第一响应者的视图

    Find View that is the firstresponder

    【讨论】:

      【解决方案3】:

      使用-[UIPickerView selectedRowInComponent:] 确定选择了哪一行,然后返回数据数组中该索引处的对象。

      【讨论】:

        【解决方案4】:
        - (void)pickerView:(UIPickerView *)pickerView1 didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
            NSInteger selectedRow = [pickerView1 selectedRowInComponent:0];
            NSString *selectedPickerRow=[yourArrayOfElements objectAtIndex:selectedRow];
        
        
        
        
        
            // Handle the selection
        }
        

        将字符串传递给文本字段

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-01-23
          • 2014-01-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多