Saturday, June 13, 2026
HomeiOS Developmentios - UIViewControllerRepresentable UIAlertController not capable of allow/disable motion?

ios – UIViewControllerRepresentable UIAlertController not capable of allow/disable motion?

[ad_1]

I wish to use an alert with textField in my swiftUI app so I made following wrapper for UIAlertController. I wish to disable my save button when there is no such thing as a textual content in textField and allow it the second consumer enters some textual content, however for some purpose it isn’t taking place, the button is caught to the preliminary state.

code

struct UIAlertControllerWrapper: UIViewControllerRepresentable {
    
    @Binding var textual content: String
    @Binding var showAlert: Bool
    
    var title: String
    var message: String
    var placeholder: String
    var motion: () -> Void
    
    func makeUIViewController(context: Context) -> some UIViewController {
        return UIViewController()
    }
    
    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
        if showAlert {
            let alert = createAlert(context)
            context.coordinator.alert = alert
            alert.actions[1].isEnabled = false
            
            DispatchQueue.fundamental.async {
                uiViewController.current(alert, animated: true) {
                    showAlert = false
                }
            }
        }
    }
    
    class Coordinator: NSObject, UITextFieldDelegate {
        var mum or dad: UIAlertControllerWrapper
        var alert: UIAlertController?
        
        init(_ mum or dad: UIAlertControllerWrapper) {
            self.mum or dad = mum or dad
        }
        
        func textField(_ textField: UITextField, shouldChangeCharactersIn vary: NSRange, replacementString string: String) -> Bool {
            if let textual content = textField.textual content as? NSString {
                mum or dad.textual content = textual content.replacingCharacters(in: vary, with: string)
            } else {
                mum or dad.textual content = ""
            }
            
            guard let alert = alert else {
                return true
            }
            
            if mum or dad.textual content.trimmingCharacters(in: .whitespaces).isEmpty {
                alert.actions[1].isEnabled = false
            } else {
                alert.actions[1].isEnabled = true
            }
            
            return true
        }
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    func createAlert(_ context: Context) -> UIAlertController {
        let alert = UIAlertController(
            title: title,
            message: message,
            preferredStyle: .alert
        )
        
        alert.addTextField { textField in
            textField.placeholder = placeholder
            textField.textual content = textual content
            textField.delegate = context.coordinator
        }
        
        let cancel = UIAlertAction(title: "Cancel", type: .cancel) { _ in
            alert.dismiss(animated: true) {
                showAlert = false
            }
            textual content = ""
        }
        
        let save = UIAlertAction(title: "Save", type: .default) { _ in
            motion()
            
            alert.dismiss(animated: true) {
                showAlert = false
            }
            textual content = ""
        }
        
        alert.addAction(cancel)
        alert.addAction(save)
        
        return alert
    }
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments