gpt4 book ai didi

ios - 如何在一个 View 中将一个 UIPickerView 用于两个文本字段?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:39:33 26 4
gpt4 key购买 nike

我在一个 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)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

if (self.aktivitiTextField.tag == 1) {

return aktivitiArray.count;

} else if (self.penganjurTextField.tag == 2) {

return penganjurArray.count;
}

return 1;

}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

if (self.aktivitiTextField.tag == 1) {
return aktivitiArray[row];
} else if (self.penganjurTextField.tag == 2) {
return penganjurArray[row];
}

return @"";
}

#pragma mark - UIPickerView Delegate

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)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) textFieldShouldReturn:(UITextField *)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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com