0

I'm new to iOS and I'm trying to make a user registration app. Code:

-(void) registerNewUser{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:_usernameField.text forKey:@"username"];
    [defaults setObject:_passwordField.text forKey:@"password"];
    [defaults setBool:YES forKey:@"registered"];
    [defaults synchronize];

    UIAlertController *success = [UIAlertController alertControllerWithTitle:@"Success"
                                message:@"You are registered"
                                preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK Action")
                                style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction *action){
                                   NSLog(@"OK Action");
                               }];
    [success addAction:okAction];
    [self presentViewController:success animated:YES completion:nil];
    [self performSegueWithIdentifier:@"login" sender:nil];

}

The issue I'm encountering is

Warning: Attempt to present <WelcomePageViewController: 0x7fa1d3cecbb0>  on <RegistrationViewController: 0x7fa1d3c49d70> which is already presenting <UIAlertController: 0x7fa1d3c5fd40>

where WelcomePageViewController is the next view controller I want to go to. I have searched through this, this and this.

Well from the fist link's accepted answer I came to know that if you have told a view controller to presentViewController:..., you cannot do that again until the presented view controller has been dismissed.

In my case during simulation I get the success you are registered message i.e up untill [self presentViewController:success animated:YES completion:nil]; but the next line [self performSegueWithIdentifier:@"login" sender:nil]; do not execute i.e the program is not segueing to next view controller since the present controller is busy with the alert controller.

When I comment out [self presentViewController:success animated:YES completion:nil]; I do go to the next view controller. But I want to get the alert message and then segue to the next view controller. How would that be possible?

Community
  • 1
  • 1
Rishab
  • 1,901
  • 2
  • 18
  • 34

1 Answers1

1

You want to perform segue after clicking on OK button, then try

- (void)registerNewUser
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:_usernameField.text forKey:@"username"];
    [defaults setObject:_passwordField.text forKey:@"password"];
    [defaults setBool:YES forKey:@"registered"];
    [defaults synchronize];

    UIAlertController *success = [UIAlertController alertControllerWithTitle:@"Success" message:@"You are registered" preferredStyle:UIAlertControllerStyleAlert]; // This statement create alert controller

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK Action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        NSLog(@"OK Action");
        [self performSegueWithIdentifier:@"login" sender:nil];
        [success dismissViewControllerAnimated:TRUE completion:nil]; // Here alert is automatically dismiss after taking action, or you can manually dismiss it by this, but not need.

    }]; // This create action for alert controller with handler. Handler is that part of action when action performed by user handler called and take action according to your code written in this block.

    [success addAction:okAction]; // Here action is added to alert controller.

    [self presentViewController:success animated:YES completion:nil]; // And this is for present or you can say show alert on your screen. here you can also add hanler instead of nil when alert present on screen
}
VRAwesome
  • 4,721
  • 5
  • 27
  • 52