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