2015-01-01 50 views
0

我有兩個不同的視圖控制器,一個用於儀表板,另一個用於註冊。我不希望用戶能夠與儀表板上的任何內容進行交互,直到用戶通過alertview進行登錄。所以每次用戶導航回儀表板或按下取消,他們都沒有登錄,我希望登錄警報彈出。viewWillAppear UIAlertView沒有顯示

在所有情況下,包括當用戶在註冊視圖中點擊導航欄上的後退按鈕,但在用戶單擊註冊頁面上的警報上的確定時都不起作用時,它可以完美地工作。

儀表板視圖包含以下代碼:

@property(strong) UIAlertView * alert; 
//... 
-(void)viewWillAppear:(BOOL)animated 
{ 
    user_email = [[NSUserDefaults standardUserDefaults] stringForKey:@"email"]; 
    if (user_email==nil){ 
     [self auto_login]; 
    } else //... 
} 

-(void)auto_login 
{ 
    alert = [[UIAlertView alloc] initWithTitle:@"Login" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Login",@"Forgot Password",@"Register",nil]; 
    alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; 
    [alert show]; 
} 

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    switch (buttonIndex) { 
     case 0: 
     { 
      self.debug.text = @"Cancel"; 
      [self auto_login]; 
      break; 
     } 
     //... 
     default: 
     { 
      self.debug.text = @"Register"; 
      [self nav_register]; 
      break; 
     } 
    } 
} 

-(void)nav_register 
{ 
    RegisterProfileController *rvc = [[RegisterProfileController alloc] init]; 
    [self.navigationController pushViewController:rvc animated:YES]; 
} 

登記視圖控制器包含這樣的代碼:

-(void)catch_registration 
{ 
    NSString *response = [[NSString alloc] initWithData:self.httpdata encoding:NSASCIIStringEncoding]; 
    if([response isEqualToString:@"OK"]){ 
     UIAlertView *successAlert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"..." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     successAlert.alertViewStyle = UIAlertViewStyleDefault; 
     [successAlert show]; 
    } 
    else //... 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if ([alertView.title isEqualToString:@"Success"]) 
     [self.navigationController popViewControllerAnimated:TRUE]; 
} 

調試之後,我知道後clickedButtonAtIndex運行在註冊視圖控制器,[alert show]運行在儀表板視圖控制器中,並且clickedButtonAtIndex不會在儀表板視圖控制器中運行,但不會顯示警報。

爲什麼警報不顯示,或者如何進一步調試?

+2

嘗試移動您的viewWillAppear代碼以viewDidAppear,因爲現在您試圖在呈現視圖之前呈現警報。 (實際上重新讀你的問題,我不是100%確定我正在理解這個問題。) –

+0

不是那個問題。我不知道有viewDidAppear方法。它現在有效 – Cbas

+0

好,很酷。很高興聽到你明白了。 :) –

回答

0

如果clickedButtonAtIndex「不運行」如您所說,那麼delegate可能沒有正確設置。另外,您應該將代碼從viewWillAppear:移至viewDidAppear:,因爲該視圖不在該點的視圖層次結構中。您的解決方案可能是這兩個問題的組合。