Search code examples
iphoneiosuialertviewdealloc

Dealloc method being called prematurely


I have an UIAlertView with 2 buttons. But I am not to able to get its clickedButtonAtIndex event. The problem is because - (void)dealloc method is being called prematurely, where I set the delegates to nil. Because of that the alert view button click could not be handled. What could be the reason for this.

Edit: My code has 2 flow directions. On one flow direction, its working fine. The dealloc method is being called in the right way. There is no problem with the view being released early.

But in the second flow, this problem arises. As of now, what I understand is that I need to set the UIAlertView delegate to [self retain] in the cases where the view gets released prematurely. But how will I check, whether the view is released or still retained, so that I don't disturb the first flow?

I am posting the relevant portion of the code. I reckon this is where the view gets deallocated.

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(actionSheet.tag == 102)
    {
        if(buttonIndex == 0)
        {
            [self.infoView removeFromSuperview]; 
            self.isSaveInfo = NO;
            [self.doneButton.target performSelector:self.doneButton.action withObject:self.doneButton.target];  //This is where the view is getting released.

            if([[NSBundle mainBundle] loadNibNamed:@"UploadView" owner:self options:nil])
            {
                [self.uploadView setFrame:CGRectMake(0, 0, 320, 480)];

                [[UIApplication sharedApplication].keyWindow addSubview:self.uploadView];
            }
            [self performSelector:@selector(RemoveView) withObject:nil afterDelay:3.0];
       }

       if(buttonIndex == 1)
       {
           [self.infoView removeFromSuperview];
           self.isSaveInfo = YES;

           [[NSNotificationCenter defaultCenter] removeObserver:self];
           [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadSaveButton) name:@"callThisMethod" object:nil];

           MyInfoViewController *myInfoViewController = [[MyInfoViewController alloc]initWithNibName:@"MyInfoViewController" bundle:nil];
           self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
           [self.navigationController myInfoViewController animated:YES];
           [myInfoViewController release];
       }
   }
}


-(void)RemoveView
{
    if([MyViewController OnSave])
    {
        self.testAlert = [[[ UIAlertView alloc]initWithTitle:messageTitle message:messageBody delegate:self cancelButtonTitle:messageClose otherButtonTitles:nil]autorelease];
        self.testAlert.tag = 1;
        [self.testAlert show];
        [MyViewController setValue:NO];
    }
    else
    {
        self.testAlert = [[[ UIAlertView alloc]initWithTitle:messageTitle message:messageBody delegate:self cancelButtonTitle:messageClose otherButtonTitles:messageTryAgain, nil]autorelease];
        self.testAlert.tag = 2;
        [self.testAlert show];
    }
}

-(void)loadSaveButton
{
    [doneButton.target performSelector:doneButton.action];

    if([[NSBundle mainBundle] loadNibNamed:@"UploadView" owner:self options:nil])
    {
        [self.uploadView setFrame:CGRectMake(0, 0, 320, 480)];
        [[UIApplication sharedApplication].keyWindow addSubview:self.uploadView];

    }
    [self performSelector:@selector(RemoveView) withObject:nil afterDelay:3.0];
}

The code inside UIActionSheet button 0 index, is where the view is getting deallocated, where as button 1 index is working fine.


Solution

  • I assume, this piece of code was the problem.

    [self performSelector:@selector(RemoveView) withObject:nil afterDelay:3.0];
    

    Here instead of withObject:nil I should have used, withObject:self, which seems to have solved the release issue.