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
不會在儀表板視圖控制器中運行,但不會顯示警報。
爲什麼警報不顯示,或者如何進一步調試?
嘗試移動您的viewWillAppear代碼以viewDidAppear,因爲現在您試圖在呈現視圖之前呈現警報。 (實際上重新讀你的問題,我不是100%確定我正在理解這個問題。) –
不是那個問題。我不知道有viewDidAppear方法。它現在有效 – Cbas
好,很酷。很高興聽到你明白了。 :) –