本文介绍了swiftUI PresentaionLink 第二次不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 swiftUI 编写的 ContentView,如下所示.

I have a ContentView written in swiftUI as simple as below.

var body: some View {

    NavigationView {
        List {
            Section {
                PresentationLink(destination: Text("new Profile")) {
                    Text("new Profile")
                }
            }
        }
    }

我第一次点击新配置文件时一切都很好,但是当我关闭模式并再次尝试点击时,它不起作用.

everything is good first time I tap on new profile but when I close the modal and try to tap again, it does not work.

这是一个错误还是一个功能?

is it a bug or a feature?

推荐答案

PresentationLink 已在 Xcode 11 beta 4 中弃用,支持 .sheet,这似乎解决了问题.

PresentationLink has been deprecated in Xcode 11 beta 4 in favor of .sheet, which seems to solve the issue.

添加了改进的演示修饰符:表(isPresented:onDismiss:内容:),actionSheet(isPresented:content:) 和 alert(isPresented:content:) —与环境中的 isPresented 一起 - 替换现有的presentation(_:)、Sheet、Modal 和 PresentationLink 类型.(52075730)

如果您将代码更改为 .sheet,如下所示:

If you change the code to .sheet like below:

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

然后,您可以根据需要多次使用模态,而不仅仅是一次.

You will then be able to use the modal as many times as you like instead of just once.

在实际场景中实现这一点后,我发现如果将 .sheet 放在 List 中,底层错误似乎仍然存在.如果您按照上面的代码示例进行操作,则不会遇到此问题,但在使用 List 的实际场景中,您可能需要有关所选特定项目的信息传入模态.在这种情况下,您将需要通过 @State var 或其他方式传递有关选择的信息.下面是一个例子:

After implementing this in a real scenario, I've found that the underlying bug still seems to exist if you put .sheet inside of the List. If you follow the code example above, you won't experience this issue but in a real scenario where you're using a List, you're probably going to want information about the particular item that was selected passed in to the modal. In that case, you're going to need to pass information about the selection via a @State var or some other means. Below is an example:

import SwiftUI

struct Testing : View {
    @State var isPresented = false
    @State var whichPresented = -1

    var body: some View {
        NavigationView {
            List {
                ForEach(0 ..< 10) { i in
                    Button(action: {
                            self.whichPresented = i
                            self.isPresented.toggle()
                })
                        { Text("Button (i)") }
                    }
                }
            }.sheet(isPresented: $isPresented, content: { Text("Destination View (self.whichPresented)") })
    }
}

这篇关于swiftUI PresentaionLink 第二次不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:59