Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Swiftui Cannot mix up a UIViewController + Complier bug problem

I have a problem that i fixed for 2 days still cannot solve. My project is like this.

struct ContentView: View{
     var body: some View{
           Controller()
     }
}

struct Controller: UIViewControllerRespresentable{
     func makeUIViewController(context: Context) -> Contro {
          let viewController = Contro()
          //additional setup
          return viewController
     }
     func updateUIViewController(_ uiViewController: Contro, context: Context) {
         //update Content
     }
}

class Contro: UIViewController{
     override func viewDidAppear(_ animated: Bool){
          let alert = UIAlertController(title: "Rename Subject", message: "", preferredStyle: .alert)
          alert.addTextField{ value in
              value.placeholder = "subject name"
              value.returnKeyType = .continue
          }
          alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
          alert.addAction(UIAlertAction(title: "Done", style: .default, handler: { _ in
            guard let fields = alert.textFields, fields.count == 1 else {
                return
            }
            guard let texts = fields[0].text, !texts.isEmpty else {
                return
            }
            UserDefaults.standard.set(texts, forKey: "RenamedSubjectName")
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.0001){
                self.dismiss(animated: true)
            }

            present(alert, animated: true)
        }
     }
}

And the compiler load and load and say: cannot build app, the compiler cannot check the error now, try to break it up to sub-expresstions

So how to solve? I will thank everyone who helped me 😀

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Replace Controller with this:

struct Controller: UIViewControllerRespresentable{
     func makeUIViewController(context: Context) -> Contro {
          let viewController = Contro()
          //additional setup
          return viewController
     }
     func updateUIViewController(_ uiViewController: Contro, context: Context) {
         //update Content
     }
}

You should use the name of your UIVIewController instead of ViewController same as contro.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading