0

I have two different view controllers, one for a dashboard and one for registration. I do not want the user to be able to interact with anything on the dashboard until the user logs in through an alertview. So every time the user navigates back to the dashboard or presses cancel and they are not logged in, I want the login alert to popup.

This works perfectly in all cases, including when the user hits the back button on the navigation bar in the registration view, but does not work when the user clicks OK on the alert in the registration page.

the dashboard view contains this code:

@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];
}

The registration view controller contains this code:

-(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];
}

After debugging, I know that after clickedButtonAtIndex runs in the registration view controller, [alert show] runs in the dashboard view controller, and clickedButtonAtIndex does NOT run in the dashboard view controller, but no alert shows up.

Why isn't the alert showing or how can I debug this further?

Cbas
  • 6,003
  • 11
  • 56
  • 87
  • 2
    Try moving your viewWillAppear code to viewDidAppear because right now you're trying to present an alert before there's a view to present it from. (Actually re-reading your question, I'm not 100% sure I'm understanding the issue.) – Lyndsey Scott Jan 01 '15 at 20:34
  • Nope that was the problem. I didn't know there was viewDidAppear method. It works now – Cbas Jan 01 '15 at 21:06
  • OK, cool. Glad to hear you figured it out. :) – Lyndsey Scott Jan 01 '15 at 21:07

1 Answers1

0

If clickedButtonAtIndex "does NOT run" as you said, then the delegate might not be set correctly. In addition, you should likely move the code from viewWillAppear: to viewDidAppear: because the view is not in the view hierarchy at that point. Your solution might be a combination of these two issues.

greg
  • 4,843
  • 32
  • 47