[英]Navigate to UIViewController from SwiftUI
My initial view is built using SwiftUI.我的初始视图是使用 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.我想从这个过渡到 UIViewController 并调用一个可能改变设计的方法,一个例子是隐藏一个 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.这是推送到我的 DetailViewController 但我
Unexpectedly found nil while implicitly unwrapping an Optional value
错误Unexpectedly found nil while implicitly unwrapping an Optional value
。
Any advice on navigating from SwiftUI to UIKit / UIViewController?关于从 SwiftUI 导航到 UIKit/UIViewController 的任何建议?
Part of working with UIViewRepresentable
is calling a required func called makeCoordinator()
as cited here in the docs.有工作的一部分
UIViewRepresentable
呼吁称之为必需的FUNC makeCoordinator()
所引用这里的文档。 Apple also has a tutorial on how to interface with UIKit which you can find here . Apple 还提供了有关如何与 UIKit 交互的教程,您可以在此处找到。 Another tutorial from Paul Hudson here might also be useful to you.
Paul Hudson 的另一个教程在这里也可能对您有用。 I used the coordinator pattern from it to provide an
UIImagePickerController
in a SwiftUI app I'm working on.我使用它的协调器模式在我正在开发的 SwiftUI 应用程序中提供了一个
UIImagePickerController
。 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.您会注意到提供
UIImagePickerController
所需功能的嵌套类,在您的情况下,我相信您只需要调用 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*/
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.