Search code examples
iphoneuipickerviewibaction

Picker View for button action getting crashed


I have a picker view,when the row component of a picker view is selected,it navigates to a view,there I have a button changeGroup.On clicking the button the picker view must appear,but it is not happening! Its crashing

Here is my code:

- (void)viewDidLoad
{
groupArr = [[[NSMutableArray alloc]initWithObjects:@"Family",@"Friends",@"Office",@"Acquaintances", nil]autorelease];
    agrpPicker.hidden  = YES;
}

#pragma mark-
#pragma mark PickerView Datasource

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    int number = 0;

    if (agrpPicker.hidden == NO) 
    {
        number = 1;
    }

    return number;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    int count = 0;
    if (agrpPicker.hidden == NO) 
    {
        count = [self.groupArr count];
    }
    return count;
}

#pragma mark-
#pragma mark PickerView Delegate

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *str = [[[NSString alloc]init]autorelease];

    if (agrpPicker.hidden == NO) 
    {
        str = [self.groupArr objectAtIndex:row];
    }

    return str;
}

This is the action for the button for the picker view to appear:

-(IBAction)groupButtonSelceted:(id)sender
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.6];
    CGAffineTransform transfrom = CGAffineTransformMakeTranslation(0, 200);
    agrpPicker.transform = transfrom;
    [UIView commitAnimations];
    agrpPicker.hidden = [agrpPicker isHidden] ? NO : YES;
    [self.view addSubview:agrpPicker];
}

connected that picker view to the picker view present in IB.

Here is the screen shot of where it got crashed

enter image description here

and in the console,the message appears as in snap shot below:

enter image description here

Can any expert point me out at where I went wrong....

Please help me out with your valuable suggestions,thanks in advance :)

Sorry guys,I found out what was the mistake,I haven't retained the array once it is autoreleased,instead of:

groupArr = [[[NSMutableArray alloc]initWithObjects:@"Family",@"Friends",@"Office",@"Acquaintances", nil]autorelease];

it is self.groupArr = ....;

Thanks for all who viewed and for those who contributed in fixing the issue :)


Solution

  • your groupArr array is loosing its contant due to autorelease. so change this

    groupArr = [[NSMutableArray alloc]initWithObjects:@"Family",@"Friends",@"Office",@"Acquaintances", nil];

    [groupArr retain];