我可以从选择器获取文本框值到一个文本框。如何获得更多文本框的不同值。目前,即使我只填写一个文本框并且只显示来自第一个 NSArray 的值,所有文本字段都会被填充。我试过一个if
说法,Apple 文档没有太大帮助。
我不知道标记是如何工作的,但注意到在研究这个时提到了它。抱歉,如果我没有正确解释这一点,如果我没有很好地解释它,请下载一个测试应用程序
。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