UIPickerView 和 UITextField 从 UIPickerView 获取值 [英] UIPickerView and UITextField get value from UIPickerView

查看:28
本文介绍了UIPickerView 和 UITextField 从 UIPickerView 获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我不知道如何解决这个问题.

Ok I am having trouble figuring out how to approach this.

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

I have a UITextField which I add to a UITableView dynamically according to the definitions of a XML file, in the XML file I may specify a list as one of the data types and what I do then is add a UIPickerView that displays the list to the inputView of my specific UITextField.

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

What I am having trouble with is figuring out how to take the selected value from the UIPickerView and add it to the text property of my UITextField

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

My UIPickerView is a custom class but I added no extra functionality I only overrode it to make it possible to pass a datasource to the UIPickerView as a property.

这是我的 PickerViewDataSourceAndDelegate.h

Here is my 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 的方式

This is how I add the UITextField with the UIPickerView as 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
            {
              ....

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

For space I omitted some of the space, if some things are unclear I will gladly explain.

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

Every thing works perfectly with no exceptions, I just want to get the selected value out of the picker

推荐答案

参见 选择器视图委托协议.实现这个方法:

See the picker view delegate protocol. Implement this method:

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

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

In here, you can get the appropriate value from the model and update your text field.

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

To work out which is the current text field, you'd need to find out which of the text fields is the first responder.

拥有 IBOutletCollection 或数组中的所有文本字段,遍历它们并找到第一响应者.

Have all of the text fields in an IBOutletCollection or array, iterate through them and find the one which is first responder.

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

Or, if you have text field delegates, you can set an ivar for the current text field in the didBeginEditing delegate method, and use this in the picker view delegate method above.

这篇关于UIPickerView 和 UITextField 从 UIPickerView 获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆