我有一个有两个按钮的UIAlertView。但是我不能得到它的clickedButtonAtIndex事件。问题是因为过早地调用了- (void)dealloc方法,我将委托设置为零。因此,无法处理警报视图按钮的单击。这可能是什么原因呢?
编辑:我的代码有两个流向。在一个流向上,它工作得很好。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索引工作得很好。
发布于 2012-06-08 13:38:43
我想,这段代码就是问题所在。
[self performSelector:@selector(RemoveView) withObject:nil afterDelay:3.0];这里,我应该使用的不是withObject:nil,而是withObject:self,这似乎已经解决了发布问题。
发布于 2012-06-06 23:35:16
如果dealloc方法被过早调用,你可能需要在某些地方保留你没有的视图,但如果不知道代码的更多细节,我们就无法告诉你它可能在哪里。
https://stackoverflow.com/questions/10917414
复制相似问题