Bạn sẽ nhận được tất cả các textfields bổ sung từ bộ điều khiển cảnh báo bằng tài sản readonly textFields
của nó, bạn có thể sử dụng nó để có được văn bản của nó. Giống như
Swift 3:
let alertController = UIAlertController(title: "", message: "", preferredStyle: .alert)
alertController.addTextField(configurationHandler: {(_ textField: UITextField) -> Void in
textField.placeholder = "Current password"
textField.isSecureTextEntry = true
})
let confirmAction = UIAlertAction(title: "OK", style: .default, handler: {(_ action: UIAlertAction) -> Void in
print("Current password \(String(describing: alertController.textFields?[0].text))")
//compare the current password and do action here
})
alertController.addAction(confirmAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {(_ action: UIAlertAction) -> Void in
print("Canelled")
})
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: { _ in })
Lưu ý: alertController.textFields [0] .text là không bắt buộc, Unwrap nó trước khi sử dụng
Objective-C:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"Current password";
textField.secureTextEntry = YES;
}];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"Current password %@", [[alertController textFields][0] text]);
//compare the current password and do action here
}];
[alertController addAction:confirmAction];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"Canelled");
}];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
By [[alertController textFields][0] text]
dòng này, nó sẽ lấy textfield đầu tiên được thêm vào alerController và nhận được văn bản của nó.
https://github.com/KiritVaghela/UIAlertController –