2012-10-15 74 views
0

我正在使用UIAlertView在下一個模式視圖之前輸入用戶名和密碼憑證,使他能夠訪問其帳戶中的文件。UIAlertView顯示比我預期的晚

但是,當我調試時,我開始知道模態視圖的「ViewDidLoad」方法在alertView出現之前被調用,因此程序崩潰,因爲視圖無法獲取用戶名和密碼。

下面是代碼

-(IBAction) openShareFile:(id)sender 
{ 
if (!self.shareFileBrowser) { 
    self.shareFileBrowser=[[ShareFileBrowserViewController alloc] initWithNibName:@"ShareFileBrowserViewController" bundle:nil]; 
    self.alertView = [[UIAlertView alloc] initWithTitle:@"Verify your Credentials" message:@"Enter Username & Password" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
    self.alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; 
    [self.alertView show]; 

    [email protected]"fidomoose"; 
    self.shareFileBrowser.delegate=self; 
    [email protected]"ShareFile Browser"; 
    self.navController= [[UINavigationController alloc] initWithRootViewController:self.shareFileBrowser]; 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 
     self.popController = [[UIPopoverController alloc] initWithContentViewController:self.navController]; 
    } 
} 

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 
    if ([self.popController isPopoverVisible]) { 
     [self.popController dismissPopoverAnimated:YES]; 
    } else { 
     [self.popController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
    } 
} 
else { 
    [self presentModalViewController:self.navController animated:YES]; 
} 

}

回答

2

只要把你的代碼視圖的初始化中的UIAlertView委託方法一樣遵循

-(IBAction) openShareFile:(id)sender 
{ 
    if (!self.shareFileBrowser) { 

     self.alertView = [[UIAlertView alloc] initWithTitle:@"Verify your Credentials" message:@"Enter Username & Password" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
     //Edit Two line inserted 

     self.alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; 
     [self.alertView show]; 
    } 
} 

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if(buttonIndex) 
    { 
     self.shareFileBrowser=[[ShareFileBrowserViewController alloc] initWithNibName:@"ShareFileBrowserViewController" bundle:nil]; 
     //Edit two line deleted 

     [email protected]"fidomoose"; 
     self.shareFileBrowser.delegate=self; 
     [email protected]"ShareFile Browser"; 
     self.navController= [[UINavigationController alloc] initWithRootViewController:self.shareFileBrowser]; 
     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 
      self.popController = [[UIPopoverController alloc] initWithContentViewController:self.navController]; 
     } 
     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 
      if ([self.popController isPopoverVisible]) { 
       [self.popController dismissPopoverAnimated:YES]; 
      } else { 
       [self.popController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
      } 
     } 
     else { 
      [self presentModalViewController:self.navController animated:YES]; 
     } 
    } 
} 

編輯:1

只需使用方法的presentPopoverFromRect:代替presentPopoverFromBarButtonItem:,並給予適當的CGRect伊娃,而不是sender

這肯定會解決你的問題:)

+0

我試過了,我得到了以下錯誤:由於未捕獲異常'NSInvalidArgumentException'而終止應用程序,原因:'應用程序試圖在目標上呈現一個無模式視圖控制器。 – jerry

+0

你的'NewMessageViewController'被釋放了一些,在它被調用之前,所以找出你在哪裏初始化視圖控制器。並只看到我的編輯。 –

+0

有一個我編輯過的錯字。我在alertView方法中錯誤地放了一些行:) –

1

你應該改變你的應用程序結構,使模式的看法不是實例(或推),直到警報視圖的DismissWithClickedButtonIndex:動畫:被調用。 。