我真的很接近这个...为此,我有两个文本框。按下时,它们会调用 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 个文本框。我试过创建另一个选择器视图,但也没有用