我在一个 View 中有 2 个文本字段。我想使用pickerview填充每个。目前,我已经成功地使一个带有一个文本字段的选择器工作正常,但是一旦我编辑了代码以使其与两个文本字段一起工作,它就无法按预期工作。任何帮助将不胜感激。
下面是我的代码:
#import "TestPickerVC.h"
@interface TestPickerVC () <UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate>
{
NSArray *aktivitiArray;
NSArray *penganjurArray;
}
@end
@implementation TestPickerVC
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.penganjurTextField.delegate = self;
self.aktivitiTextField.delegate = self;
aktivitiArray = @[@"apple", @"samsung", @"motorola", @"nokia"];
penganjurArray = @[@"jimbit", @"ayam", @"kambing", @"emergency"];
// self.aktivitiTextField.inputView = self.pickerView1;
// self.penganjurTextField.inputView = self.pickerView2;
self.pickerView = [[UIPickerView alloc] init];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
self.aktivitiTextField.inputView = self.pickerView;
self.penganjurTextField.inputView = self.pickerView;
self.aktivitiTextField.tag = 1;
self.penganjurTextField.tag = 2;
// if (self.aktivitiTextField.tag == 100) {
// self.aktivitiTextField.inputView = self.pickerView;
// self.pickerView.tag = 1;
// NSLog(@"pickerview tag : %d", (int)self.pickerView.tag);
// } else if (self.penganjurTextField.tag == 200) {
// self.penganjurTextField.inputView = self.pickerView;
// self.pickerView2.tag = 2;
// NSLog(@"pickerview tag : %d", (int)self.pickerView2.tag);
//
// }
}
#pragma mark - UIPickerView DataSource
- (NSInteger)numberOfComponentsInPickerViewUIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerViewUIPickerView *)pickerView numberOfRowsInComponentNSInteger)component {
if (self.aktivitiTextField.tag == 1) {
return aktivitiArray.count;
} else if (self.penganjurTextField.tag == 2) {
return penganjurArray.count;
}
return 1;
}
- (NSString *)pickerViewUIPickerView *)pickerView titleForRowNSInteger)row forComponentNSInteger)component {
if (self.aktivitiTextField.tag == 1) {
return aktivitiArray[row];
} else if (self.penganjurTextField.tag == 2) {
return penganjurArray[row];
}
return @"";
}
#pragma mark - UIPickerView Delegate
- (void)pickerViewUIPickerView *)pickerView didSelectRowNSInteger)row inComponentNSInteger)component {
NSString *selectedAktivitiRow = aktivitiArray[row];
NSString *selectedPenganjurRow = penganjurArray[row];
if (self.aktivitiTextField.tag == 1) {
self.aktivitiTextField.text = selectedAktivitiRow;
} else if (self.penganjurTextField.tag == 2) {
self.penganjurTextField.text = selectedPenganjurRow;
}
}
-(BOOL) textFieldShouldReturnUITextField *)textField{
[textField resignFirstResponder];
return YES;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
创建另一个私有(private)变量作为 selectedTextField。
@interface TestPickerVC () <UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate>
{
NSArray *aktivitiArray;
NSArray *penganjurArray;
int selectedTextField
}
点击文本字段的那一刻,文本字段委托(delegate)方法 textFieldDidBeginEditing
将被调用。在这个方法中,做 -
selectedTextField = textField.tag;
现在在选择器委托(delegate)方法中,检查这个条件:
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (selectedTextField == 1) {
return aktivitiArray.count;
} else if (selectedTextField == 2) {
return penganjurArray.count;
}
return 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (selectedTextField == 1) {
return aktivitiArray[row];
} else if (selectedTextField == 2) {
return penganjurArray[row];
}
return @"";
}
对所有选择器委托(delegate)方法都这样做。
关于ios - 如何在一个 View 中将一个 UIPickerView 用于两个文本字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34022967/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |