Enable UIAlertAction of UIAlertController only after input

前端 未结 6 1697
感情败类
感情败类 2020-12-16 01:39

I am using a UIAlertController to present a dialog with a UITextField and one UIAlertAction button labeled \"Ok\". How do I disable th

6条回答
  •  轮回少年
    2020-12-16 01:42

    With Swift 5.3 and iOS 14, you can use Combine framework and NotificationCenter to track UITextField.textDidChangeNotification notifications for a given UITextField.


    The following code shows a possible implementation in order to enable the button of a UIAlertController according to the character count of its textField:

    import UIKit
    import Combine
    
    class ViewController: UIViewController {
    
        var cancellable: AnyCancellable?
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            view.backgroundColor = .systemBackground
    
            let action = UIAction(
                title: "Change title",
                handler: { [unowned self] _ in
                    self.presentAlert()
                }
            )
            let barButtonItem = UIBarButtonItem(primaryAction: action)
            navigationItem.rightBarButtonItem = barButtonItem
        }
    
    }
    
    extension ViewController {
    
        func presentAlert() {
            let alertController = UIAlertController(
                title: "Change title",
                message: nil,
                preferredStyle: .alert
            )
            let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { _ in
                print("Cancelled")
            }
            let renameAction = UIAlertAction(
                title: "Rename",
                style: .default
            ) { [unowned alertController] action in
                print("Renamed: \(alertController.textFields!.first!.text!)")
            }
            renameAction.isEnabled = false
    
            alertController.addAction(cancelAction)
            alertController.addAction(renameAction)
            alertController.addTextField(configurationHandler: { textField in
                self.cancellable = NotificationCenter.default
                    .publisher(for: UITextField.textDidChangeNotification, object: textField)
                    .sink(receiveValue: { _ in
                        let textCount = textField.text?.trimmingCharacters(in: .whitespacesAndNewlines).count ?? 0
                        renameAction.isEnabled = textCount >= 5 // min 5 characters
                    })
            })
            present(alertController, animated: true)
        }
    
    }
    

提交回复
热议问题