无法用图像解决双选择器

时间:2012-02-11 00:29:53

标签: iphone cocoa-touch uipickerview

我正在尝试做双重选择器,一部分是文本,另一部分是图像。但是代码给了我一个错误:Thread 1: Program received signal: "EXC_BAD_ACCESS"。我看不出麻烦。这是代码,Array content Images和Array2内容文本。感谢。

@synthesize Array, picker, Array2;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    UIImage *one = [UIImage imageNamed:@"6-12AM.png"];
    UIImageView *oneView = [[UIImageView alloc] initWithImage:one];
    NSArray *Array1 = [[NSArray alloc] initWithObjects:oneView, nil];
    NSString *fieldName = [[NSString alloc] initWithFormat:@"Array"];
    [self setValue:Array1 forKey:fieldName];
    Array2 = [[NSArray alloc] initWithObjects:@"Hello", @"trouble", nil];
    [super viewDidLoad];

}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{

        return 2;// giving number of components in PickerView

    }

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

    return [self.Array2 count];// counting the number of rows in each component

}

#pragma mark Picker Delegate Methods

- (UIView *)pickerView:(UIPickerView *)pickerView 
            viewForRow:(NSInteger)row
          forComponent:(NSInteger)component 
           reusingView:(UIView *)view 
{
    if (component == 1) {


        return [self.Array objectAtIndex:row];
    }
}   //In this line is where the error occurs

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row     forComponent:(NSInteger)component; {
    if (component == 0) {
        return [self.Array2 objectAtIndex:row];
    }
}

2 个答案:

答案 0 :(得分:2)

您的错误是由于您没有提供if语句的替代方法。非void函数必须在每种情况下返回一些东西,因此你需要在两个函数中的每一个函数中提供一个else语句,它返回一个值(如nil)。

答案 1 :(得分:0)

我没有看到你分配或初始化Array。因此访问不好。你的意思是使用Array1吗?

还有几件事:

  1. 内存分配了几个从未取消分配的地方。全身泄漏
  2. 变量名称应以array1,array2
  3. 等小型大写字母开头
  4.  [self setValue:Array1 forKey:fieldName];   //what is this doing?