• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-13 12:11:37 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我在一个 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



Best Answer-推荐答案


创建另一个私有(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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap