如何使用UIPicker与多个文本字段

时间:2012-07-30 11:02:27

标签: objective-c ios xcode textbox uipickerview

我可以从选取器到一个文本框获取文本框值。如何为更多文本框获取不同的值。目前,即使我只填写一个文本框并且只显示第一个NSArray的值,所有文本字段也会被填充。我尝试了if声明,Apple文档帮助不大。

我不知道标记是如何工作的,但注意到在研究这个时就提到过了。如果我没有正确解释这个问题,请致歉下载一个测试应用程序下载,如果我有很多意义,可以更好地解释它

APP example

·H

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIPickerViewDataSource,    UIPickerViewDelegate,UITextFieldDelegate>

{
//decalre picker
UIPickerView *select;



//declare NSArrray
NSArray *arrStatus;
NSArray *arrStatus2;


 }

@property (strong, nonatomic) IBOutlet UITextField *text1;
 - (IBAction)Value:(id)sender;
@property (strong, nonatomic) IBOutlet UITextField *text2;
@end

的.m

     - (void)viewDidLoad
 {



///create picker//////////////////////////////////////////////////////////////////////////

select = [[UIPickerView alloc] initWithFrame:CGRectZero];
select.delegate = self;
select.dataSource = self;
[select setShowsSelectionIndicator:YES];
text1.inputView = select;
text2.inputView = select;



////arrays & objects
arrStatus = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",nil];
arrStatus2 = [[NSArray alloc] initWithObjects:@"A",@"B",@"C",@"D",nil];

//number of colums and rows etc of picker    
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
//One column
return 1;
}

 -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
//set number of rows
return arrStatus.count;
 return arrStatus2.count;

  }

  -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
 {
//set item per row
return [arrStatus objectAtIndex:row];
return [arrStatus2 objectAtIndex:row];


- (IBAction)Value:(id)sender {

///get picker values to text field
NSInteger selectedRow = [select selectedRowInComponent:0];

text1.text = [arrStatus objectAtIndex: selectedRow];
[text1 resignFirstResponder];

text2.text = [arrStatus2 objectAtIndex: selectedRow];
[text2 resignFirstResponder];


 }

 @end

1 个答案:

答案 0 :(得分:1)

在didselectrow的方法中我有我的if语句,看看哪个文本字段是第一个响应者。然后使用以下代码。它将uipicker值放在每个文本字段中。

-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if ([_input1 isFirstResponder])
{
    NSString *resultString1 = [[NSString alloc] initWithFormat:[gradeArray objectAtIndex:[pickerView selectedRowInComponent:0]]];
    NSString *resultString2 = [[NSString alloc] initWithFormat:[plusMinusArray objectAtIndex:[pickerView selectedRowInComponent:1]]];
    NSString *gradeResult = [resultString1 stringByAppendingString:resultString2];
    _input1.text = gradeResult;
}else if ([_input2 isFirstResponder])
{
    NSString *resultString1 = [[NSString alloc] initWithFormat:[gradeArray objectAtIndex:[pickerView selectedRowInComponent:0]]];
    NSString *resultString2 = [[NSString alloc] initWithFormat:[plusMinusArray objectAtIndex:[pickerView selectedRowInComponent:1]]];
    NSString *gradeResult = [resultString1 stringByAppendingString:resultString2];
    _input2.text = gradeResult; 
}else if ([_inputM isFirstResponder])
{
    NSString *resultString1 = [[NSString alloc] initWithFormat:[gradeArray objectAtIndex:[pickerView selectedRowInComponent:0]]];
    NSString *resultString2 = [[NSString alloc] initWithFormat:[plusMinusArray objectAtIndex:[pickerView selectedRowInComponent:1]]];
    NSString *gradeResult = [resultString1 stringByAppendingString:resultString2];
    _inputM.text = gradeResult;
}else if ([_input3 isFirstResponder])
{
    NSString *resultString1 = [[NSString alloc] initWithFormat:[gradeArray objectAtIndex:[pickerView selectedRowInComponent:0]]];
    NSString *resultString2 = [[NSString alloc] initWithFormat:[plusMinusArray objectAtIndex:[pickerView selectedRowInComponent:1]]];
    NSString *gradeResult = [resultString1 stringByAppendingString:resultString2];
    _input3.text = gradeResult;
}else if ([_input4 isFirstResponder])
{
    NSString *resultString1 = [[NSString alloc] initWithFormat:[gradeArray objectAtIndex:[pickerView selectedRowInComponent:0]]];
    NSString *resultString2 = [[NSString alloc] initWithFormat:[plusMinusArray objectAtIndex:[pickerView selectedRowInComponent:1]]];
    NSString *gradeResult = [resultString1 stringByAppendingString:resultString2];
    _input4.text = gradeResult;
}
}

希望这有帮助。

修改

如果要在选择器中获取两个数组,则需要创建第2列。这将显示两个数组。我使用以下代码在选择器视图中显示我的数组。

-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0)
{
    return [gradeArray count];
}
return [plusMinusArray  count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0)
{
    return  [gradeArray objectAtIndex:row];
}
return [plusMinusArray objectAtIndex:row];
}