-1

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

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

文本框两个返回值 A、B、C、D

问题在于,当对文本框两个提出挑选器时,它将显示文本框一号的值,尽管将从文本框两个中选择适当的行。有人可以检查我的代码并告诉我我在哪里出错,谢谢。

。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 个文本框。我试过创建另一个选择器视图,但也没有用

4

2 回答 2

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

希望这可以帮助。干杯!

于 2012-07-30T12:30:42.423 回答
0

这是能够显示具有给定数组的选择器视图的代码,文本字段将具有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];
}
于 2012-07-30T12:33:09.573 回答