是否已替换过时的PresentationLink?(Xcode 11 beta 4)

KRH*_*KRH 8 swiftui

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

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

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

但我不确定如何切换到此模式-特别是,这种isPresented说法使我感到困惑。我知道有一个名为isPresented的Environment变量,但是对于当前View来说,这不是吗,不是要呈现的View吗?

我对此最感兴趣,因为我希望这能解决PresentationLinks仅工作一次的问题(请参阅swiftUI PresentaionLink第二次不工作

现在不赞成使用PresentationLink的人可以提供一个简单的示例来显示视图吗?例如,将以下内容转换为使用.sheet修饰符:

  NavigationView {
        List {
            PresentationLink(destination: Text("Destination View")) {
                Text("Source View")
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

Zai*_*ain 5

下面是一个与我可能接近的示例。

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") })
    }
}
Run Code Online (Sandbox Code Playgroud)

实际上,这确实可以解决您所引用的有关PresentationLinks的错误,该错误第二次不起作用


MAV*_*AVO 4

您应该能够像这样更新您的代码,

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")
            }
        }


    }
}

Run Code Online (Sandbox Code Playgroud)

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