如何在选择器视图中获取多个数组

时间:2012-07-30 12:24:52

标签: objective-c ios xcode nsarraycontroller picker

我真的很接近这个...为了这个目的,我有两个文本框。按下时,他们会调用一个ui选择器。

文本框1返回值1,2,3,4

文本框2返回值A,B,C,D

问题在于,当文本框2显示选择器时,它会显示文本框1中的值,但会从文本框2中选择适当的行。有人可以查看我的代码并告诉我在哪里发生错误,谢谢。

·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

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize text2;

@synthesize text1;

- (void)viewDidLoad
{



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


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


-(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];



[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

 - (void)viewDidUnload
{
[self setText1:nil];
[self setText2:nil];

[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)Value:(id)sender {




NSInteger selectedRow = [select selectedRowInComponent:0];

if([text1 isFirstResponder])
text1.text = [arrStatus objectAtIndex: selectedRow];
[text1 resignFirstResponder];

if ([text2 isFirstResponder])
text2.text = [arrStatus2 objectAtIndex: selectedRow];
[text2 resignFirstResponder];


}

@end

总而言之,我试图获得,1个选择器视图,1个列,2个数组,2个文本框。香港专业教育学院尝试创建另一个选择器视图,但它没有工作

2 个答案:

答案 0 :(得分:3)

return语句退出方法。因此,如果您有2个return语句,它将永远不会到达第二个语句。您应该修改这些方法:

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
 {
    if([text1 isFirstResponder])
        return arrStatus.count;
    else
        return arrStatus2.count;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
     if([text1 isFirstResponder])
         return [arrStatus objectAtIndex:row];
     else
         return [arrStatus2 objectAtIndex:row];
}

希望这会有所帮助。干杯!

答案 1 :(得分:0)

这是Code能够显示给定数组的选择器视图,textfield将显示 UIPickerView 的第一个数据。如果您有任何问题请告诉我。我做了一个下一个文本字段的文本字段,您只需要使用文本字段的标记找出并设置文本。

-(void)viewDidLoad
    {
        [super viewDidLoad];

        arrayNo = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",nil];
        [pickerView selectRow:1 inComponent:0 animated:NO];
        self.text1.text= [arrayNo objectAtIndex:[pickerView selectedRowInComponent:0]];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    mlabel.text=    [arrayNo objectAtIndex:row];
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
    return [arrayNo count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    return [arrayNo objectAtIndex:row];
}