【问题标题】:Replacement for Deprecated PresentationLink? (Xcode 11 beta 4)替换已弃用的 PresentationLink? (Xcode 11 测试版 4)
【发布时间】:2019-11-26 19:21:53
【问题描述】:

在 Xcode beta 4 中使用 PresentationLink 会给出以下警告:“'PresentationLink' 已弃用:请改用 .sheet 修饰符。”

我假设它们的意思是某种形式的

func sheet<Content>(isPresented: Binding<Bool>, onDismiss: (() -> Void)? = nil, content: @escaping () -> Content) -> some View where Content : View

但我不确定如何切换到这个 - 特别是 isPresented 参数让我感到困惑。我知道有一个名为 isPresented 的环境变量,但这不是针对当前视图,而不是将要呈现的视图吗?

我对此最感兴趣,因为我希望这将解决 PresentationLinks 仅工作一次的问题(请参阅swiftUI PresentaionLink does not work second time)

任何人都可以提供一个简单的例子来说明在 PresentationLink 已被弃用的情况下如何呈现视图?例如,将以下内容转换为使用 .sheet 修饰符:

  NavigationView {
        List {
            PresentationLink(destination: Text("Destination View")) {
                Text("Source View")
            }
        }
    }

【问题讨论】:

  • 这是一个错误。请提交FB6525020的欺骗文件。

标签: swiftui


【解决方案1】:

你应该可以像这样更新你的代码,

struct MainScreen: View {
    @State var shown = false

    var body: some View {

        VStack{
            Button(action: {
                self.shown.toggle()
            }) {
                Text("Press me to present")
            }
        }.sheet(isPresented: $shown) { () -> SecondScreen in

            return SecondScreen(dismissFlag: self.$shown)
        }


    }
}


struct SecondScreen: View {

    @Binding var dismissFlag: Bool

    var body: some View {

        VStack{
            Button(action: {

                self.dismissFlag = false

            }) {
                Text("Second screen, click to exit")
            }
        }


    }
}

关于环境变量 isPresented,您可以使用该方法,并且您应该能够在 SecondScreen 视图中设置 isPresented?.value = false,但是我无法在 beta 4 中使用它我在 beta 3 中使用过这种方法就好了。

【讨论】:

  • 这是一个错误。请提交FB6525020 的欺骗文件。
【解决方案2】:

下面是一个尽可能接近您的示例的示例。

import SwiftUI

struct Testing : View {
    @State var isPresented = false

    var body: some View {
        NavigationView {
            List {
                Button(action: { self.isPresented.toggle() })
                    { Text("Source View") }
                }
            }.sheet(isPresented: $isPresented, content: { Text("Destination View") })
    }
}

这似乎确实解决了您提到的关于 PresentationLinks not working the second time 的错误

【讨论】:

  • 我认为.sheet 参数应该附加到Button 而不是List,并且$isPresented 应该在前面加上self. 我的代码中有错误,直到我做出这些调整。
  • @Ben 如果您将 .sheet 附加到 Button,您可能会遇到此错误 stackoverflow.com/a/57087399/3179416
【解决方案3】:

我会使用这些代码直到目前的方法稳定?

UIApplication.shared.windows[0].rootViewController!.present(UIHostingController(rootView: view), animated: true, completion: nil)
UIApplication.shared.windows[0].rootViewController!.dismiss(animated: true, completion: nil)

(在使用VStack 或List 时,.sheet 似乎损坏或难以使用)

【讨论】:

  • 拯救了我余下的一天!
【解决方案4】:

我建议使用导航链接

NavigationView {
  List {
    NavigationLink(
      destination: Text("Destination View")) {
        Text("Source View")
   }
  }
}

更多详情来自this文章

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多