2015-11-19 103 views
0

我是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進行了搜索。

那麼從拳頭鏈接的接受的答案我知道,如果你已經告訴視圖控制器presentViewController:...,你不能再這樣做,直到提出的視圖控制器已被解僱。

在模擬過程中我來說,我得到了success you are registered消息即高達直到[self presentViewController:success animated:YES completion:nil];但下一行[self performSegueWithIdentifier:@"login" sender:nil]; 不執行即程序未segueing到下一個視圖控制器由於本控制器正忙於報警控制器。

當我註釋掉[self presentViewController:success animated:YES completion:nil];我確實轉到下一個視圖控制器。但我想獲得警報消息,然後繼續到下一個視圖控制器。這怎麼可能?

回答

1

你想點擊OK按鈕後執行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 
} 
+0

它的工作。我可以問你解釋流程是如何發生的嗎? – Rishab

+0

請參閱答案中的評論。高興地幫助:) – VRAwesome

+0

最少15個字符是怎麼回事。謝謝btw。 – Rishab

相關問題