1

我在 Xcode 4.4 中遇到了选择器视图的问题。我想用多个数组填充 1 Picker。我看了/阅读了很多教程,几乎可以开始工作了。这是我的代码:

*。H

 #import <UIKit/UIKit.h>

@interface PickerView : UIViewController

<UIPickerViewDataSource,UIPickerViewDelegate>
{
    IBOutlet UIPickerView *picker;

    NSMutableArray *array_1;
    NSMutableArray *array_2;
}

@property (strong, nonatomic) IBOutlet UIView *pickerContainer;
@property (strong, nonatomic) IBOutlet UITextField *textfield_1;
@property (strong, nonatomic) IBOutlet UITextField *textfield_2;

- (IBAction)showArray_1Picker:(id)sender;
- (IBAction)showArray_2Picker:(id)sender;
- (IBAction)hidePicker:(id)sender;

@end

*.m

#import "PickerView.h"

@interface PickerView ()

@end

@implementation PickerView
@synthesize pickerContainer;
@synthesize textfield_1;
@synthesize textfield_2;

int textFieldTouched = 0;

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent (NSInteger)component
{
    switch(textFieldTouched)
    {
        case 1:
            return [array_1 count];
            break;
        case 2:
            return [array_2 count];
            break;
        default:
            return 0;
    }
}


- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch(textFieldTouched)
    {
        case 1:
            return [array_1 objectAtIndex:row];
            break;
        case 2:
            return [array_2 objectAtIndex:row];
            break;
        default:
            return @"nothing";
    }
}

- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    switch(textFieldTouched)
    {
        case 1:
            textfield_1.text = [array_1 objectAtIndex:[pickerView selectedRowInComponent:0]];
            break;
        case 2:
            textfield_2.text = [array_2 objectAtIndex:[pickerView selectedRowInComponent:0]];
            break;
    }
}

 - (void)viewDidLoad
 {
    array_1 = [[NSMutableArray alloc] init];
    [array_1 addObject:@"1"];
    [array_1 addObject:@"2"];
    [array_1 addObject:@"3"];
    [array_1 addObject:@"4"];
    [array_1 addObject:@"5"];

    array_2 = [[NSMutableArray alloc] init];
    [array_2 addObject:@"a"];
    [array_2 addObject:@"b"];
    [array_2 addObject:@"c"];
    [array_2 addObject:@"d"];
    [array_2 addObject:@"e"];

    pickerContainer.frame = CGRectMake(0, 460, 320, 261);

    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [self setPickerContainer:nil];
    [self setTextfield_1:nil];
    [self setTextfield_2:nil];
    [super viewDidUnload];
}

- (IBAction)showArray_1Picker:(id)sender
{
    [textfield_1 resignFirstResponder];
    textFieldTouched = 1;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    pickerContainer.frame = CGRectMake(0, 200, 320, 261);
    [UIView commitAnimations];
}

- (IBAction)showArray_2Picker:(id)sender
{
    [textfield_2 resignFirstResponder];
    textFieldTouched = 2;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    pickerContainer.frame = CGRectMake(0, 200, 320, 261);
    [UIView commitAnimations];
}

- (IBAction)hidePicker:(id)sender
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    pickerContainer.frame = CGRectMake(0, 460, 320, 261);
    [UIView commitAnimations];
}

@end

所以我遇到了两个主要问题:

  1. 如果我设置 textFieldTouched = 0 (或 > 2),就像我在选择器出现时为空之前所做的那样。无论如何,如果我再次隐藏它,它会在文本字段中写入相应数组的第一个值。

  2. 如果我设置 textFieldTouched = 1 或 =2,无论我尝试编辑什么文本字段,选择器都会填充相关数组。但是,如果我将选择器上的值滚动到我的视线之外,当我向后滚动时它们会发生变化。

我想要这样:

如果我触摸文本字段 1,选择器会显示填充数组 1

如果我触摸文本字段 2,选择器会显示填充数组 2

等等。

你能帮我吗?

我已经上传了代码,以便您更好地查看它。

https://dl.dropbox.com/u/5858884/ProblemCode.zip

附言

英语不是我的母语,所以请原谅错误;)

我两周前开始编程,所以如果你在我的代码中发现不属于我的问题的错误,请告诉我,这样我会变得更好:)

4

1 回答 1

2

在您的 showArray1Picker 和 showArray2Picker 方法中添加以下行:

[picker reloadAllComponents];

这将导致选择器重新加载其数据。您还需要将 IBOutlet 选择器连接到 IB 中的选择器视图——我注意到它没有连接。

于 2012-09-07T23:40:29.887 回答