我有一个 UIAlertView
有 2 个按钮。但我无法得到它的clickedButtonAtIndex
事件。问题是因为 - (void)dealloc
方法被过早调用,我将代表设置为零。因此,无法处理警报 View 按钮单击。这可能是什么原因。
编辑:
我的代码有 2 个流向。在一个流动方向上,它工作正常。以正确的方式调用了 dealloc 方法。 View 提前发布没有问题。
但是在第二个流程中,出现了这个问题。截至目前,我的理解是我需要将 UIAlertView 委托(delegate)设置为 [self retain]
在 View 过早释放的情况下。但是我将如何检查 View 是否被释放或仍然保留,以免打扰第一个流程?
我正在发布代码的相关部分。我认为这是 View 被释放的地方。
- (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 索引,是 View 被释放的位置,按钮 1 索引工作正常。
最佳答案
如果过早地调用了 dealloc 方法,那么您可能需要在某个地方保留您没有保留的 View 。但是,如果不知道您的代码的更多详细信息,我们无法告诉您它可能在哪里。
关于iphone - 过早调用 Dealloc 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10917414/