【问题标题】:How to pop a UIViewControllerRepresentable from a SwiftUI navigation stack programmatically如何以编程方式从 SwiftUI 导航堆栈中弹出 UIViewControllerRepresentable
【发布时间】:2020-09-18 10:50:29
【问题描述】:

这是我的 SwiftUI 设置:

我有一个 SwiftUI 视图(称为 MapSearchView),它有一个包含 NavigationLink 的 NavigationView。

    var body: some View {
    VStack {
        NavigationView {
            Form {
                Section (header: Text("Location")){
                    NavigationLink (locationString, destination: GooglePlacesViewController(mapTools: $mapTools))
                }...

NavigationLink 的目的地是一个 UIViewControllerRepresentable,它包装了一个 GooglePlacesViewController。当我完成 GooglePlaces 搜索(因为我进行了选择或取消了搜索)后,我想以编程方式弹回 MapSearchView。

我尝试在 UIViewControllerRepresentable (GooglePlacesViewController) 中添加以下内容:

    @Environment(\.presentationMode) var presentationMode

var popToPrevious : Bool = false {
    didSet {
        if popToPrevious == true {
            self.presentationMode.wrappedValue.dismiss()
        }
    }
}

并在我的协调器中使用

将 popToPrevious 设置为 true
parent.popToPrevious = true

它确实被调用了,但什么也不做。

我也试过在Coordinator中调用

view.viewController().navigationController.popViewController(animated: true)

(viewController() 是获取任何视图的 viewController 的扩展)

这也无济于事。

想法?

【问题讨论】:

    标签: swift swiftui navigationview navigationlink uiviewcontrollerrepresentable


    【解决方案1】:

    如果您使用所有者视图(即您的可代表)创建协调器,则在需要时尝试直接调用

    ownerView.presentationMode.wrappedValue.dismiss()
    

    【讨论】:

    • 谢谢。我在做这个的时候一定是睡着了,因为这是显而易见的答案,我之前已经做过好几次了!再次感谢您的帮助!
    猜你喜欢
    • 2018-04-12
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 2020-04-12
    • 2022-07-31
    • 1970-01-01
    • 2013-02-19
    • 2023-03-22
    相关资源
    最近更新 更多