默认的navigationController中嵌入视图控制器并推送到下一个UIHostingController的快捷项目。
如何从navigationController?.popViewController调用SimpleView?
ViewController:
@IBOutlet weak var button: UIButton?
override func viewDidLoad() {
super.viewDidLoad()
button?.addTarget(self,
action: #selector(showNewSwiftUIController),
for: .touchUpInside)
}
@objc func showNewSwiftUIController() {
let vc = UIHostingController(rootView:SimpleView())
navigationController?.pushViewController(vc, animated: true)
}SwiftUI:
struct SimpleView: View {
var body: some View {
Text("SimpleView")
.foregroundColor(.black)
}
}发布于 2022-04-28 22:25:54
第一。你试过吗?
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode> // reference然后在你的身体呼叫:
self.presentationMode.wrappedValue.dismiss()如果这样做不起作用:
我建议您将导航控制器作为可选变量传递到View中,作为引用并从那里进行访问。其他选项(也许是更健康的选项)是将一个“回调”变量从父变量传递给childView,以关闭视图(您需要将导航保存为一个变量来跟踪)
最后一种选择(不建议)
if var topController = UIApplication.shared.windows.first?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
topController.navigationController.popViewController(animated:true)
}https://stackoverflow.com/questions/57094702
复制相似问题