iphone - UIpickerView 在同一个 ScrollView 中填充多个 uitextFields

标签 iphone objective-c uiscrollview uitextfield uipickerview

嗨,我正在开发一款 iPhone 应用程序,我在其中填充了 uipickerview,它与一个 uitextfield 配合使用效果很好。但我需要填充 6 个 uitextfield,并且我为每个 UItextfield 使用相同的 uipickerview。

我正在使用加载了数组对象的 Ipickerview,并且当触摸每个字段时它会弹出。问题在于 UItextfields 下面的代码共享来自选择器的相同数据。

我不知道如何编码,以便每个字段从 UIPickerView 行获取自己的数据。 我究竟做错了什么?有编码建议吗?

谢谢

@实现BestViewController

-(NSInteger)numberOfComponentsInPickerView: (UIPickerView *)thePickerView 
{ return 1; }

-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:                    (NSInteger)component 
{return [list count]; }

-(NSString *)pickerView:(UIPickerView *)thePickerview titleForRow:(NSInteger)row  forComponent:(NSInteger)component 
{return [list objectAtIndex:row]; }

-(void)pickerView:(UIPickerView *)thePickerview didSelectRow:(NSInteger)row inComponent:      (NSInteger)component 
{ 
    uitextfield1.text = [list objectAtIndex:row];   
    utitextfield2.text = [list objectAtIndex:row];
}



- (void)viewDidLoad {

[super viewDidLoad];

    uitextfield1.inputView = pickerView; 
uitextfield2.inputView = pickerView;

list = [[NSMutableArray alloc] init];   
[list addObject:@"a"];
[list addObject:@"b"];
    [list addObject:@"c"];  
    [list addObject:@"d"];
}    

最佳答案

您需要获取当前的第一响应者并设置其 text 属性,而不是显式设置特定的文本字段。

所以,

-(void)pickerView:(UIPickerView *)thePickerview didSelectRow:(NSInteger)row inComponent:      (NSInteger)component 
{ 
    // textFields is an NSArray holding each of the textfields that are using the picker as an input view
    for (UITextField textField in textFields)
    {
        if ([textField isFirstResponder])
        {
            textField.text = [list objectAtIndex:row];
            break;
        }
    }    
}

可能有一种更优雅的方式来找到当前的第一响应者,这超出了我的想象。 textFields 可能是一个 IBOutletCollection,但我自己没有使用过它们,所以我不能有太多权威。

关于iphone - UIpickerView 在同一个 ScrollView 中填充多个 uitextFields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7139789/

相关文章:

ios - UICollectionViewCell 在 iOS7 和 iOS 8 上的大小不一样

ios - ShareKit 是否仍然有效 - 2015?如果可以,是否可以在 Swift 中实现?

ios - 展开可选值 (UIButton) 时意外发现 nil

iPhone:滚动时展开 UITextView

objective-c - UIScrollView 和取消缩放手势

iphone - UITabBar 中的 UITabBarItems 在我单击该项目后显示,而不是在应用程序启动时显示

iphone - 关于自动释放/释放使用的另一个 iOS 内存管理说明

iphone - 从代码打开 map 应用程序 - 在哪里/如何找到 "Current Location"?

objective-c - _PFBatchFaultingArray objectAtIndex :

iphone - Cocos2D随机 Sprite 运动