swiftUI PresentaionLink第二次不起作用 [英] swiftUI PresentaionLink does not work second time

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

问题描述

我有一个用swiftUI编写的ContentView,如下所示. ''' var body:一些视图{

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:content :), actionSheet(isPresented:content :)和alert(isPresented:content :) — 以及isPresented在环境中-替换现有的 Presentation(_ :),Sheet,Modal和PresentationLink类型. (52075730)

Added improved presentation modifiers: sheet(isPresented:onDismiss:content:), actionSheet(isPresented:content:), and alert(isPresented:content:) — along with isPresented in the environment — replace the existing presentation(_:), Sheet, Modal, and PresentationLink types. (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第二次不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆