1

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

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

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

我正在发布代码的相关部分。我认为这是视图被释放的地方。

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

按钮 0 索引内的代码UIActionSheet是视图被释放的地方,而按钮 1 索引工作正常。

4

2 回答 2

3

如果过早地调用了 dealloc 方法,那么您可能需要在某个地方保留您没有保留的视图。但是,如果不知道您的代码的更多详细信息,我们无法告诉您它可能在哪里。

于 2012-06-06T15:35:15.940 回答
0

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

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

在这里,我应该使用 withObject:self 而不是 withObject:nil,这似乎解决了发布问题。

于 2012-06-08T05:38:42.917 回答