Dealloc方法被过早调用

时间:2012-06-06 15:32:17

标签: iphone ios uialertview dealloc

我有一个带有2个按钮的UIAlertView。但我无法得到它的clickedButtonAtIndex事件。问题是因为- (void)dealloc方法被过早调用,我将委托设置为nil。因此无法处理警报视图按钮单击。可能是什么原因。

编辑: 我的代码有2个流向。在一个流动方向,它的工作正常。以正确的方式调用dealloc方法。早期发布的视图没有问题。

但是在第二个流程中,出现了这个问题。截至目前,我理解的是,在视图过早发布的情况下,我需要将UIAlertView委托设置为[self retain]。但是我如何检查视图是否被释放或仍保留,以便我不打扰第一个流程?

我发布了代码的相关部分。我认为这是视图被取消分配的地方。

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

UIActionSheet按钮0索引中的代码,是视图被取消分配的位置,其中按钮1索引正常工作。

2 个答案:

答案 0 :(得分:3)

如果过早调用dealloc方法,可能需要在某处保留视图而不是。但是,如果不了解代码的更多细节,我们无法告诉您可能的位置。

答案 1 :(得分:0)

我认为,这段代码就是问题所在。

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

这里代替withObject:nil我应该使用withObject:self,这似乎解决了发布问题。