0

我有两个不同的视图控制器,一个用于仪表板,一个用于注册。在用户通过警报视图登录之前,我不希望用户能够与仪表板上的任何内容进行交互。因此,每次用户导航回仪表板或按下取消并且他们没有登录时,我都希望弹出登录警报。

这在所有情况下都能完美运行,包括当用户在注册视图中点击导航栏上的后退按钮时,但当用户在注册页面的警报上单击“确定”时不起作用。

仪表板视图包含以下代码:

@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中运行,并且不在仪表板视图控制器中运行,但没有显示警报。

为什么不显示警报或如何进一步调试?

4

1 回答 1

0

如果clickedButtonAtIndex如您所说“未运行”,则delegate可能未正确设置。此外,您可能应该将代码从 移动viewWillAppear:到 ,viewDidAppear:因为此时视图不在视图层次结构中。您的解决方案可能是这两个问题的组合。

于 2015-01-01T20:36:08.463 回答