简体   繁体   English

UIPickerView和UITextField从UIPickerView获取值

[英]UIPickerView and UITextField get value from UIPickerView

Ok I am having trouble figuring out how to approach this. 好的,我无法弄清楚如何处理这个问题。

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. 我有一个UITextField,我根据XML文件的定义动态添加到UITableView,在XML文件中我可以指定一个列表作为数据类型之一,然后我做的是添加一个显示列表的UIPickerView给我的特定UITextField的inputView。

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获取所选值并将其添加到我的UITextField的text属性中

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. 我的UIPickerView是一个自定义类,但我没有添加任何额外的功能我只覆盖它,以便可以将数据源作为属性传递给UIPickerView。

Here is my PickerViewDataSourceAndDelegate.h 这是我的PickerViewDataSourceAndDelegate.h

#import <Foundation/Foundation.h>

@interface PickerViewDataSourceAndDelegate : UIPickerView <UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) NSMutableArray *pickerData;

@end

And it's .m file 它是.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

This is how I add the UITextField with the UIPickerView as inputView 这就是我将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
            {
              ....

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. 要确定哪个是当前文本字段,您需要找出哪个文本字段是第一个响应者。

Have all of the text fields in an IBOutletCollection or array, iterate through them and find the one which is first responder. 拥有IBOutletCollection或数组中的所有文本字段,遍历它们并找到第一个响应者。

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. 或者,如果您有文本字段委托,则可以在didBeginEditing委托方法中为当前文本字段设置ivar,并在上面的选择器视图委托方法中使用它。

I found my own solution that words like a charm, I had to create a delegate method to pass the value back to my UITextField. 我发现自己的解决方案,像魅力,我必须创建一个委托方法将值传递回我的UITextField。

First I declared a protocol in my PickerViewDataSourceAndDelegate.h 首先,我在PickerViewDataSourceAndDelegate.h中声明了一个协议

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

Then I added a member: 然后我添加了一个成员:

id <PickerRowSelected> adelegate;

and added a property 并添加了一个属性

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

I then added this function 然后我添加了这个功能

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

And in the controller that contains the textfield I added: 在包含我添加的文本字段的控制器中:

pickerDataDel.adelegate = self;

And implemented my new delegate method 并实现了我的新委托方法

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

See my answer on this question for finding the view that is the first responder 请参阅我在这个问题上的答案,找到第一个响应者的视图

Find View that is the firstresponder 查找作为第一响应者的视图

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

- (void)pickerView:(UIPickerView *)pickerView1 didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
    NSInteger selectedRow = [pickerView1 selectedRowInComponent:0];
    NSString *selectedPickerRow=[yourArrayOfElements objectAtIndex:selectedRow];





    // Handle the selection
}

Pass the String to the Text Field 将字符串传递给文本字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM