2016-05-31 52 views
-1

我用一些文本框和兩個按鈕製作UIAlertController。我有按鈕註冊。當我點擊按鈕註冊時如何保持UIAlertController。因爲我在按鈕中運行一些方法後,我的UIAlertController關閉。點擊按鈕註冊後,請幫我保留UIAlertController。我如何驗證UIAlertController中的textfield?單擊按鈕時如何保持UIAlertController?

UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"Login" 
                                 message: @"Input username and password" 
                               preferredStyle:UIAlertControllerStyleAlert]; 
               [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
                textField.placeholder = @"username"; 
                textField.textColor = [UIColor blueColor]; 
                textField.clearButtonMode = UITextFieldViewModeWhileEditing; 
                textField.borderStyle = UITextBorderStyleRoundedRect; 
                textField.text=username; 
                textField.userInteractionEnabled = NO; 

               }]; 
               [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
                textField.placeholder = @"email"; 
                textField.textColor = [UIColor blueColor]; 
                textField.clearButtonMode = UITextFieldViewModeWhileEditing; 
                textField.borderStyle = UITextBorderStyleRoundedRect; 
                textField.text=email; 
                textField.userInteractionEnabled = NO; 

               }]; 
               [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
                textField.placeholder = @"tell"; 
                textField.textColor = [UIColor blueColor]; 
                textField.clearButtonMode = UITextFieldViewModeWhileEditing; 
                textField.borderStyle = UITextBorderStyleRoundedRect; 
               }]; 
               [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
                textField.placeholder = @"address"; 
                textField.textColor = [UIColor blueColor]; 
                textField.clearButtonMode = UITextFieldViewModeWhileEditing; 
                textField.borderStyle = UITextBorderStyleRoundedRect; 
               }]; 
               [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
                textField.placeholder = @"city"; 
                textField.textColor = [UIColor blueColor]; 
                textField.clearButtonMode = UITextFieldViewModeWhileEditing; 
                textField.borderStyle = UITextBorderStyleRoundedRect; 
               }]; 
               [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
                textField.placeholder = @"national"; 
                textField.textColor = [UIColor blueColor]; 
                textField.clearButtonMode = UITextFieldViewModeWhileEditing; 
                textField.borderStyle = UITextBorderStyleRoundedRect; 
               }]; 
               [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { 
                textField.placeholder = @"postcode"; 
                textField.textColor = [UIColor blueColor]; 
                textField.clearButtonMode = UITextFieldViewModeWhileEditing; 
                textField.borderStyle = UITextBorderStyleRoundedRect; 
               }]; 
               [alertController addAction:[UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
                n++; 
                FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init]; 
                [loginManager logOut]; 
                [sender setTitle:@"Login with facebook" forState:UIControlStateNormal]; 

               }]]; 
               [alertController addAction:[UIAlertAction actionWithTitle:@"Register" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 
                NSArray * textfields = alertController.textFields; 
                UITextField * usernamefield = textfields[0]; 
                UITextField * emailfield = textfields[1]; 
                UITextField * tellfield = textfields[2]; 
                UITextField * addressfield = textfields[3]; 
                UITextField * cityfield = textfields[4]; 
                UITextField * nationalfield = textfields[5]; 
                UITextField * postcodefield = textfields[6]; 
                //NSLog(@"%@:%@:%@:%@:%@:%@",usernamefield.text,emailfield.text,tellfield.text, 
                //  addressfield.text,cityfield.text,nationalfield.text,postcodefield.text); 



                NSInteger success=0; 
}]]; 

               [self presentViewController:alertController animated:YES completion:nil]; 

請幫幫我!謝謝!

+1

這看起來像一個[XY問題](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)。警報的目的是提供快速的交互或信息。爲什麼你需要一個不會消失的警報? – Arc676

+0

因爲當我輸入錯誤的數據,我點擊按鈕註冊我的UIAlertController將關閉。所以我想保持UIAlertController並顯示驗證錯誤,當我輸入錯誤的數據。 –

+0

a)您可能需要一個單獨的窗口。您可以使用界面生成器b)或者,您可以隨時顯示另一個警報 – Arc676

回答

1

默認情況下是不可能的。因爲默認行爲會取消操作按鈕單擊時的警報控制器。

根據您的需要,您應該使用tableview或自定義視圖來管理註冊。具有文本框和按鈕或表視圖

顯示自定義的視圖來實現這一

Alert controller不是這樣的任務。它僅代表alertviewactionsheet,並且兩者的默認行爲均爲點擊關閉。

Alert controller沒有任何委託方法也不像alertviewactionsheet。所以很難用警報控制器進行管理。

然後,如果您只想使用警報控制器,那麼您可以顯示其操作的完成處理程序中的另一個控制器。

您應該保留先前填充的文本字段的記錄,以在警報控制器上再次顯示它。

希望這會有所幫助:)

+0

謝謝你,我再次調用[self presentViewController:alertController animated:YES completion:nil];當我想再次顯示警報控制器時。 –

+0

不客氣.... :) – Lion

0

是的,您不能更改Alertcontroller的默認行爲。您必須使用視圖註冊,或者您必須提供另一個來自操作的警報。