【发布时间】: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