Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iOS的UIAlertView中添加3个UITextFields?_Ios_Objective C_Uitextfield_Uialertview - Fatal编程技术网

如何在iOS的UIAlertView中添加3个UITextFields?

如何在iOS的UIAlertView中添加3个UITextFields?,ios,objective-c,uitextfield,uialertview,Ios,Objective C,Uitextfield,Uialertview,我想在UIAlertView中添加3个文本字段,如oldpassword、Newpassword、confirmpassword,如下所示。 这是我试过的代码 -(IBAction)changePswd:(id)sender { UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Change Password" message:@"Enter Your New Password" delegate:self cancelBu

我想在
UIAlertView
中添加3个文本字段,如oldpassword、Newpassword、confirmpassword,如下所示。 这是我试过的代码

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

它只显示两个文本字段。

为此使用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];
}

为此使用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];
}

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

注:

swift 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)
}
注意:

这是正确的方法:

let title = "Your Title"
let message = "Your message"
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
现在添加文本字段

alertController.addTextField { (textField : UITextField!) -> Void in
    textField.placeholder = "Your placeholder..."
        textField.tintColor = .yourColor
        textField.secureTextEntry = true
    }
    alertController.addTextField { (textField2 : UITextField!) -> Void in
        textField2.placeholder = "Your placeholder2..."
        textField2.tintColor = . yourColor
        textField2.secureTextEntry = true
    }
添加第一个操作

let firstAction = UIAlertAction(title: "Save Text", style: .default, handler: { alert -> Void in
        guard let textField = alertController.textFields else { return }
        let firstTextField = textField[0] as UITextField
        let secondTextField = textField[1] as UITextField
        //insert your code here
        print(secondTextField.text ?? "") 
        print(firstTextField.text ?? "")
    })
添加取消操作

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (cancel) in
        print("Action cancelled")
    }
向警报控制器添加操作

alertController.addAction(firstAction)
alertController.addAction(cancelAction)
现在出现警报控制器

self.present(alertController, animated: true, completion: nil)
将此代码放入函数中,使用参数进行修改,并在需要时调用它…

这是正确的方法:

let title = "Your Title"
let message = "Your message"
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
现在添加文本字段

alertController.addTextField { (textField : UITextField!) -> Void in
    textField.placeholder = "Your placeholder..."
        textField.tintColor = .yourColor
        textField.secureTextEntry = true
    }
    alertController.addTextField { (textField2 : UITextField!) -> Void in
        textField2.placeholder = "Your placeholder2..."
        textField2.tintColor = . yourColor
        textField2.secureTextEntry = true
    }
添加第一个操作

let firstAction = UIAlertAction(title: "Save Text", style: .default, handler: { alert -> Void in
        guard let textField = alertController.textFields else { return }
        let firstTextField = textField[0] as UITextField
        let secondTextField = textField[1] as UITextField
        //insert your code here
        print(secondTextField.text ?? "") 
        print(firstTextField.text ?? "")
    })
添加取消操作

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (cancel) in
        print("Action cancelled")
    }
向警报控制器添加操作

alertController.addAction(firstAction)
alertController.addAction(cancelAction)
现在出现警报控制器

self.present(alertController, animated: true, completion: nil)

将此代码放入函数中,使用参数进行修改,并在需要时调用它…

您的目标操作系统是什么?您应该查看UIAlertControlle:可能的副本您应该制作一个自定义警报以添加文本字段,只需制作一个自定义UIView类并使其看起来像您想要的。然后只需在类中显示它,而不是调用UIAlertView。感谢您的回复:)您的目标是什么操作系统?您应该查看UIAlertControlle:可能的副本您应该制作一个自定义警报以添加文本字段,只需制作一个自定义UIView类并使其看起来像您想要的。然后在类中显示它,而不是调用UIAlertView。感谢您的回复:)在这方面,有必要添加委托方法吗?没有。这只是为了,如果你想在收到用户的文本后执行任何操作。如果这个答案有帮助,那么接受并投票,这样其他用户就可以发现它是有帮助的。谢谢:)用户在文本字段中输入文本,我如何使用POST方法将数据存储到数据库中?在这种情况下,有必要添加委托方法?否。这只是为了,如果你想在收到用户的文本后执行任何操作。如果这个答案有帮助,那么接受并投票,这样其他用户就可以发现它是有帮助的。谢谢:)用户在文本字段中输入文本,如何使用POST方法将数据存储到数据库中?