2015-12-29 15 views
25

enter image description hereLàm thế nào để thêm textField trong UIAlertController?

enter image description here

Tôi muốn thực hiện một chức năng về việc thay đổi mật khẩu, nó đòi hỏi người dùng phải nhập mật khẩu trước đây của ông, và tôi thiết kế nó trong một hộp thoại cảnh báo, tôi muốn nhấn vào nút "Xác nhận việc sửa đổi "sau đó nhảy đến bộ điều khiển xem khác để thay đổi mật khẩu. Tôi đã viết một số mã, nhưng tôi không biết làm thế nào để viết trong thời điểm tiếp theo.

+0

https://github.com/KiritVaghela/UIAlertController –

Trả lời

22

Bạn có thể thêm nhiều textfields để cảnh báo điều khiển và truy cập chúng với tài sản textfields cảnh báo điều khiển của

Nếu mật khẩu mới là chuỗi rỗng, trình bày các cảnh báo một lần nữa. Hoặc một cách khác .. vô hiệu hóa nút Confirm đầu tiên, cho phép nó chỉ khi trường văn bản có văn bản

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"confirm the modification" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { 
    UITextField *password = alertController.textFields.firstObject; 
    if (![password.text isEqualToString:@""]) { 

     //change password 

    } 
    else{ 
     [self presentViewController:alertController animated:YES completion:nil]; 
    } 
}]; 
+0

ke y là làm thế nào để có được textfield trong alertcontroller.So tôi nghĩ rằng dòng này là rất quan trọng! UITextField * password = alertController.textFields.firstObject; Khi tôi biết mã này, tôi có thể viết mã tiếp theo. – Juice007

+0

Cách tắt nút Xác nhận – iOSGeek

+0

Tham chiếu 'alertController' bên trong khối của hành động dẫn đến chu kỳ lưu giữ. Bạn chỉ nên tham khảo nó thông qua tham chiếu yếu: '__weak typeof (alertController) weakAlert = alertController;' ... 'UIAlertAction * ...' '... password = weakAlert.textFields.firstObject;' – beebcon

39

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ó.

+0

Tôi muốn biết cái gì mã này "[alertController textFields] [0] text" có nghĩa là không? – Juice007

+1

@ Juice007 nó có nghĩa là bạn đang nhận được textfield đầu tiên của bộ điều khiển cảnh báo và gettings văn bản của nó, khá đơn giản –

+1

bạn NÊN sử dụng '[weak alertController]' để tránh một chu kỳ giữ lại. – dOM

7

Dưới đây là một câu trả lời được cập nhật cho Swift 4.0 tạo ra các loại mong muốn của textfield:

// Create a standard UIAlertController 
let alertController = UIAlertController(title: "Password Entry", message: "", preferredStyle: .alert) 

// Add a textField to your controller, with a placeholder value & secure entry enabled 
alertController.addTextField { textField in 
    textField.placeholder = "Enter password" 
    textField.isSecureTextEntry = true 
    textField.textAlignment = .center 
} 

// A cancel action 
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { _ in 
    print("Canelled") 
} 

// This action handles your confirmation action 
let confirmAction = UIAlertAction(title: "OK", style: .default) { _ in 
    print("Current password value: \(alertController.textFields?.first?.text ?? "None")") 
} 

// Add the actions, the order here does not matter 
alertController.addAction(cancelAction) 
alertController.addAction(confirmAction) 

// Present to user 
present(alertController, animated: true, completion: nil) 

Và làm thế nào nó trông khi lần đầu tiên trình bày: enter image description here

Và khi chấp nhận văn bản:

enter image description here

Các vấn đề liên quan