使用Xcode11GM种子编写一些SwiftUI代码,我遇到了一个我不理解的Swift错误。
struct MainViewController: View {
var body: some View {
VStack {
Text("Hello World!")
}
}
}
extension MainViewController : UIViewControllerRepresentable {
func makeUIViewController(context: UIViewControllerRepresentableContext<MainViewController>) -> UINavigationController {
return UINavigationController()
}
func updateUIViewController(_ uiViewController: UINavigationController, context: UIViewControllerRepresentableContext<MainViewController>) {
}
}这将报告:
'UIViewControllerRepresentable' requires the types 'some View' and 'Never' be equivalent发布于 2019-09-12 19:58:13
我错过了ViewController和视图之间的分离。错误是说视图控制器不能有返回视图的主体。
这是可行的:
struct MainView : View {
var body: some View {
VStack {
Text("Hello World!")
}
}
}
struct MainViewController : UIViewControllerRepresentable {
func makeUIViewController(context: UIViewControllerRepresentableContext<MainViewController>) -> UIHostingController<MainView> {
return UIHostingController(rootView: MainView())
}
func updateUIViewController(_ uiViewController: UIHostingController<MainView>, context: UIViewControllerRepresentableContext<MainViewController>) {
}
}然后实例化它:
let viewController = UIHostingController<MainViewController>(rootView:MainViewController())https://stackoverflow.com/questions/57905693
复制相似问题