Enrique Marquez
Enrique Marquez

Reputation: 23

Can't solve the double picker with images

I'm trying to do a double picker, one part of text and the other with images. But the code gives me an error: Thread 1: Program received signal: "EXC_BAD_ACCESS". I can't see the trouble. Here is the code, Array content Images and Array2 content text. Thanks.

@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];
    }
}

Upvotes: 0

Views: 156

Answers (2)

John Roberts
John Roberts

Reputation: 5966

Your error is due to the fact that you do not offer an alternative to the if statement. A non-void function must return something in every case, so you need to provide an else statement in each one of your two functions that returns a value (like nil).

Upvotes: 2

mbh
mbh

Reputation: 3312

I dont see you allocating or initializing Array. Hence the bad access. Did you mean to use Array1?

Also several things:

  1. Memory allocated several places never deallocated. Leaks all over
  2. Variable names should start with small caps like array1, array2
  3.  [self setValue:Array1 forKey:fieldName];   //what is this doing?
    

Upvotes: 0

Related Questions