在调用警报控制器后调用下一个视图控制器

时间:2015-11-19 12:30:47

标签: ios objective-c uiviewcontroller

我是iOS新手,我正在尝试制作用户注册应用。代码:

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

}

我遇到的问题是

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

其中WelcomePageViewController是我想要去的下一个视图控制器。我搜索了thisthisthis

从第一个链接接受的答案开始,我开始知道如果你已经告诉视图控制器来呈现ViewController:...,在呈现的视图控制器被解除之前你不能再这样做了。

在模拟过程中,我得到了success you are registered消息,即直到[self presentViewController:success animated:YES completion:nil];,但是下一行[self performSegueWithIdentifier:@"login" sender:nil]; 没有执行,即程序没有从现在开始到下一个视图控制器控制器忙于警报控制器。

当我评论[self presentViewController:success animated:YES completion:nil];时,我会转到下一个视图控制器。但我想获取警报消息,然后转到下一个视图控制器。怎么可能?

1 个答案:

答案 0 :(得分:1)

您想在单击“确定”按钮后执行segue,然后尝试

- (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
}
相关问题