2016-01-13 67 views
0

我想在UIAlertView中添加3个TextField,如oldpassword,Newpassword,confirmpassword像这样。 这里是我的代码,我试图如何在iOS中将3个UITextFields添加到UIAlertView中?

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

[如何将多个UITextField添加到iOS 7中的UIAlertView?](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)以文本字段值

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) 
} 

说明:

相关问题