2012-06-06 66 views
1

我有一個UIAlertView帶有2個按鈕。但我無法得到它的clickedButtonAtIndex事件。問題是因爲- (void)dealloc方法被過早調用,我將代理設置爲nil。因爲警報視圖按鈕點擊無法處理。這可能是什麼原因。過早調用Dealloc方法

編輯: 我的代碼有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個索引工作正常。

+0

代理人在警報視圖之前消失是否有意義?雖然不是真正的模態,但UIAlertView看起來像一個模態視圖,旨在用作模態視圖。從這個角度(並做出一些假設,隨時糾正它們),當警報視圖不再需要時(從視圖層次中彈出),委派(創建警報視圖?)不再需要顯示。 – pmf

+0

我編輯了這個問題,並更新了一些代碼 –

回答

0

我認爲,這段代碼是問題所在。

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

在這裏,而不是withObject:無我應該使用withObject:self,這似乎已經解決了發佈問題。

3

如果過早調用dealloc方法,則可能有某處需要保留視圖的位置。儘管不知道代碼的更多細節,但我們不能告訴你可能在哪裏。

+0

它是一個相當大的類,這就是爲什麼我沒有發佈代碼。所以保留這個觀點似乎是現在的正確選擇?並保持其中的觀點,一個良好的編碼習慣? –

相關問題