1

如何使用 UIViewControllerRepresentable 编写以下代码?我想使用 UIViewControllerRepresentable。我希望我编写的代码是常规的。你觉得用 UIViewControllerRepresentable 写这段代码有意义吗?

 import MessageUI
    
    class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {
    
        @IBOutlet weak var phoneNumber: UITextField!
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    
        @IBAction func sendText(sender: UIButton) {
            if (MFMessageComposeViewController.canSendText()) {
                let controller = MFMessageComposeViewController()
                controller.body = "Message Body"
                controller.recipients = [phoneNumber.text]
                controller.messageComposeDelegate = self
                self.presentViewController(controller, animated: true, completion: nil)
            }
        }
    
        func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
            //... handle sms screen actions
            self.dismissViewControllerAnimated(true, completion: nil)
        }
    
        override func viewWillDisappear(animated: Bool) {
            self.navigationController?.navigationBarHidden = false
        }
    }

短信视图控制器

 struct SMSViewController: UIViewControllerRepresentable {
        
        class Coordinator: NSObject, MFMessageComposeViewControllerDelegate {
            
            func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
                
            }
            var parent: MFMessageComposeViewController
            
            init(_ parent: MFMessageComposeViewController) {
                self.parent = parent
            }
        }
        
        func makeCoordinator() -> Coordinator {
            Coordinator(self)
        }
        
        func makeUIViewController(context: UIViewControllerRepresentableContext<MFMessageComposeViewController>) -> MFMessageComposeViewController {
            let messageViewController = MFMessageComposeViewController()
            messageViewController.delegate = context.coordinator
            return messageViewController
        }
        func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
            
        }
    }
4

0 回答 0