2012-03-26 69 views
0

好吧我無法弄清楚如何處理這個問題。UIPickerView和UITextField從UIPickerView中獲取值

我有一個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 

這就是我如何與UIPickerView添加的UITextField作爲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 
      { 
       .... 

對於空間我省略了一些空間,如果有些事情不清楚,我會很樂意解釋。

每一件事情完美的作品,沒有例外,我只是想選擇的值了選擇器的

回答

1

請參閱picker view delegate protocol。實施此方法:

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

在這裏,您可以從模型中獲取適當的值並更新文本字段。

要確定哪些是當前文本字段,您需要找出哪些文本字段是第一響應者。

讓IBOutletCollection或數組中的所有文本字段遍歷它們並找到第一個響應者。

或者,如果您有文本字段委託,則可以在didBeginEditing委託方法中爲當前文本字段設置ivar,並在上面的選擇器視圖委託方法中使用它。

+0

我的問題是如何傳遞的觀點它回到我的文本字段,因爲我可以有多個文本字段在其inputView中具有pickerviews,所以我不能一切都動態構建 – Armand 2012-03-26 14:43:52

+0

查看更新answe r – jrturton 2012-03-26 15:15:21

+0

你的編輯與我最終做的事情非常相似,我會接受你的回答,因爲它會使我朝着正確的方向走向最終的目標。現在我會按照我的方式保持它,直到我完成演示,然後我可以再優化一下。謝謝 – Armand 2012-03-26 15:21:50

0

使用-[UIPickerView selectedRowInComponent:]確定選擇哪一行,然後你的數據數組內的索引返回對象。

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





    // Handle the selection 
} 

將字符串傳遞到文本字段

1

我發現我自己的解決方案,它像一個魅力的話,我不得不創建一個委託方法傳遞值回我的UITextField。

首先我在PickerViewDataSourceAndDelegate中聲明瞭一個協議。ħ

@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