2016-01-13 2 views
0

Я хочу добавить 3 TextFields в UIAlertView как oldpassword, Newpassword, confirmpassword вот так. Вот мой код я попыталсяКак добавить 3 UITextFields в UIAlertView в iOS?

-(IBAction)changePswd:(id)sender 
{ 
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Change Password" message:@"Enter Your New Password" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"submit", nil]; 
[alertView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput]; 
[[alertView textFieldAtIndex:0]setSecureTextEntry:YES]; 
[alertView textFieldAtIndex:0].keyboardType = UIKeyboardTypeDefault; 
[alertView textFieldAtIndex:0].returnKeyType = UIReturnKeyDone; 
[[alertView textFieldAtIndex:0]setPlaceholder:@"Enter Your Old Password"]; 
[[alertView textFieldAtIndex:1]setPlaceholder:@"Enter password"]; 
[[alertView textFieldAtIndex:2]setPlaceholder:@"Re-Enter Password"]; 
[alertView show]; 
} 

это показывает только два текстовых поля.

+0

Что вы планируете использовать? Вы должны посмотреть на UIAlertControlle: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/ –

+0

Возможный дубликат [Как добавить несколько UITextFields в UIAlertView в iOS 7?] (Http://stackoverflow.com/questions/19090547/how-to-add-multiple-uitextfields-to-a-uialertview-in-ios-7) –

+0

Вы должны создать настраиваемое оповещение для добавления текстовых полей, просто создайте пользовательский класс UIView и сделайте это похоже на то, что вы хотите. Затем просто покажите его в своем классе, а не вызовите UIAlertView. –

ответ

4

Для этого используйте UIAlertController.

UIAlertController *alertController = [UIAlertController 
             alertControllerWithTitle:title 
             message:message 
           preferredStyle:UIAlertControllerStyleAlert]; 

__block typeof(self) weakSelf = self; 

//old password textfield 
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) 
{ 
    textField.tag = 1001; 
    textField.delegate = weakSelf; 
    textField.placeholder = @"Old Password"; 
    textField.secureTextEntry = YES; 
    [textField addTarget:weakSelf action:@selector(alertTextFieldDidChange:) 
     forControlEvents:UIControlEventEditingChanged]; 
}]; 

//new password textfield 
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) 
{ 
    textField.tag = 1002; 
    textField.delegate = weakSelf; 
    textField.placeholder = @"New Password"; 
    textField.secureTextEntry = YES; 

    }]; 

//confirm password textfield 
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) 
{ 
    textField.tag = 1003; 
    textField.delegate = weakSelf; 
    textField.placeholder = @"Confirm Password"; 
    textField.secureTextEntry = YES; 
    [textField addTarget:weakSelf action:@selector(alertTextFieldDidChange:) 
     forControlEvents:UIControlEventEditingChanged]; 
}]; 

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

#pragma mark - UITextField Delegate Methods 
- (void)alertTextFieldDidChange:(UITextField *)sender 
{ 
    alertController = (UIAlertController *)self.presentedViewController; 
    UITextField *firstTextField = alertController.textFields[0]; 
} 
+0

В этом необходимо добавить методы делегата? –

+0

Нет. Его просто для, если вы хотите выполнить любое действие после получения текста от пользователя. – iOSEnthusiatic

+0

ОК. спасибо :) –

0

быстры 2,0:

1) Создание общественной переменной

var alertController: UIAlertController? 

2) Настройка оповещения Контроллер

alertController = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: .Alert) 

    //old password textfield 
    alertController!.addTextFieldWithConfigurationHandler({(textField: UITextField!) in 
     textField.placeholder = "Password" 
     textField.secureTextEntry = true 
     textField.addTarget(self, action: "alertTextFieldDidChange:", forControlEvents: .EditingChanged) 
    }) 
    //new password textfield 
    alertController!.addTextFieldWithConfigurationHandler({(textField: UITextField) -> Void in 
     textField.tag = 1002 
     textField.placeholder = "New Password" 
     textField.secureTextEntry = true 
     textField.addTarget(self, action: "alertTextFieldDidChange:", forControlEvents: .EditingChanged) 
    }) 
    //confirm password textfield 
    alertController!.addTextFieldWithConfigurationHandler({(textField: UITextField) -> Void in 
     textField.tag = 1003 
     textField.placeholder = "Confirm Password" 
     textField.addTarget(self, action: "alertTextFieldDidChange:", forControlEvents: .EditingChanged) 
    }) 
    alertController!.addAction(UIAlertAction(title: "Submit", style: UIAlertActionStyle.Default, handler: { (action) -> Void in 
     //Do after submit is clicked 
    })) 
alertController!.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: { (action) -> Void in 
    self.alertController?.dismissViewControllerAnimated(true, completion: nil) 
})) 
    self.presentViewController(alertController!, animated: true, completion: nil) 

3) Принимая TextField значения

func alertTextFieldDidChange(sender: UITextField) { 
    alertController = self.presentedViewController as? UIAlertController 
    let firstTextField: UITextField = (alertController?.textFields![0])! 
    let secondTextField: UITextField = (alertController?.textFields![1])! 
    let thirdTextField: UITextField = (alertController?.textFields![2])! 

    print(firstTextField.text) 
    print(secondTextField.text) 
    print(thirdTextField.text) 
} 

Примечание:

Смежные вопросы