My initial view is built using SwiftUI.
I'd like to transition to a UIViewController from this & call a method that may alter the design, an example is hiding an IBOutlet.
Here's what I am doing currently:
@available(iOS 13.0, *)
struct DetailViewControllerWrapper: UIViewControllerRepresentable {
typealias UIViewControllerType = DetailViewController
func makeUIViewController(context: UIViewControllerRepresentableContext<DetailViewControllerWrapper>) -> DetailViewControllerWrapper.UIViewControllerType {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let detailViewController: DetailViewController = mainStoryboard.instantiateViewController(withIdentifier: "detailViewController") as! DetailViewController
detailViewController.someMethod()
return detailViewController
}
func updateUIViewController(_ uiViewController: DetailViewControllerWrapper.UIViewControllerType, context: UIViewControllerRepresentableContext<DetailViewControllerWrapper>) {
//
}
}
This is pushing to my DetailViewController but I'm getting a Unexpectedly found nil while implicitly unwrapping an Optional value
error.
Any advice on navigating from SwiftUI to UIKit / UIViewController?
Part of working with UIViewRepresentable
is calling a required func called makeCoordinator()
as cited here in the docs. Apple also has a tutorial on how to interface with UIKit which you can find here . Another tutorial from Paul Hudson here might also be useful to you. I used the coordinator pattern from it to provide an UIImagePickerController
in a SwiftUI app I'm working on. That code is below. You'll notice the nested class which provides the functionality I need for the UIImagePickerController
, in your case I believe you just need to call the DetailViewController. I hope this helps. -Dan
import SwiftUI
class PhotosData: NSObject, Codable {
var image: String?
init(image: String) {
self.image = image
}
}
struct ImagePicker: UIViewControllerRepresentable {
@Environment(\.presentationMode) var presentationMode
@Binding var image: UIImage?
class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
let parent: ImagePicker
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let uiImage = info[.originalImage] as? UIImage {
parent.image = uiImage
}
parent.presentationMode.wrappedValue.dismiss()
}
init(_ parent: ImagePicker) {
self.parent = parent
}
}
func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
let picker = UIImagePickerController()
picker.delegate = context.coordinator
return picker
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<ImagePicker>) {
/*Required. Not used*/
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.