2011-10-25 34 views
0

我在一類下面的代碼,雖然警報會出現在UI(使用的是iOS SDK 5.0)當視圖即將消失,clickedButtonAtIndex方法不會被調用和該應用終止於「EXC_BAD_ACCESS」。我驗證了視圖是/將我的課程作爲代理使用。UIAlertView中內部viewWillDisappear並沒有叫clickedButtonAtIndex

代碼位於主線程上,在查看了關於此主題的所有其他響應之後,我無法明白爲什麼我的委託方法從未被調用過。我可以使用另一個線索人。

@interface ConnectionViewController : UIViewController <UIAlertViewDelegate> { 
      .... 
    } 

@implementation ConnectionViewController 
... 

    - (void)viewWillDisappear:(BOOL)animated 
    { 
     connection = [Connection objectWithConnName:[connectionName text] host:[mtDevice text] user:[userName text] passwd:[userPassword text]]; 
     BOOL result = [connection test]; 
     if (result) { 
      [[FirstViewController sharedInstance] addConnection:connection];  
     } else { 
      UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"No Connection" message:@"Failed to connect to device" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ignore", @"Ok", nil]; 
      [alert show]; 
     } 
    } 

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
    { 
     NSLog(@"clickedButtonAtIndex: %d",buttonIndex); 
    } 

回答

1

警告視圖不會阻止 - 基本上,當您選擇一個選項時,您的視圖控制器已經看到viewWillDisappear,viewDidDisappear,並可能dealloc,這意味着它不再存在。您正在使用一個UINavigationController,如果這個想法是提示導航回到之前的用戶假設,你應該重寫

- (UINavigationItem *)popNavigationItemAnimated:(BOOL)animated; 
{ 
    MyAppDelegateName* delegate = (MyAppDelegateName*)[[UIApplication sharedApplication] delegate]; 
    if([delegate.navigationController.topViewController conformsToProtocol:@protocol(ExitConfirmDelegate)]) { 
     if([(UIViewController<ExitConfirmDelegate>*)delegate.navigationController.topViewController shouldConfirmExit]) { 
      return; 
     } 
     [delegate.navigationController popViewControllerAnimated:animated]; 
    } 
} 
在UINavigationBar的地方ExitConfirmDelegate是BOOL shouldConfirmExit協議

。您的視圖控制器將實現此協議,並且如果可以看到待處理的警報視圖,則返回「否」。然後,當用戶點擊一個選項時,只需從clickedButtonAtIndex方法中再次調用popViewControllerAnimated即可。

+0

我知道它一定與阻塞有關,謝謝! – tgunr

0

確保ConnectionViewController不釋放之前被稱爲 「clickedButtonAtIndex」 的方法。