繁体   English   中英

Dealloc方法被过早调用

[英]Dealloc method being called prematurely

我有一个带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索引正常工作。

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

我假设,这段代码是问题所在。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM