Ara*_*ash 7 modal-dialog swiftui
我有一个用swiftUI编写的ContentView,如下所示。'''var body:some View {
NavigationView {
List {
Section {
PresentationLink(destination: Text("new Profile")) {
Text("new Profile")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
'''
第一次点击新配置文件,一切都很好,但是当我关闭模态并尝试再次点击时,它不起作用。它是错误还是功能?
Zai*_*ain 11
PresentationLink已在Xcode 11 beta 4中弃用,而推荐使用.sheet,似乎可以解决此问题。
添加了改进的表示修饰符:sheet(isPresented:onDismiss:content :),actionSheet(isPresented:content :)和alert(isPresented:content:)(与环境中的isPresented一起)-替换现有的presentation(_ :),Sheet,模态和PresentationLink类型。(52075730)
如果将代码更改为.sheet,如下所示:
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)
然后,您将可以根据需要多次使用该模式,而不仅仅是一次。
编辑:在真实的场景中实现在此之后,我发现下面的错误似乎仍然如果你把存在.sheet的内部List。如果您遵循上面的代码示例,则不会遇到此问题,但在使用的真实情况下List,您可能会希望将有关所选特定项目的信息传递给模式。在这种情况下,您将需要通过@Statevar或其他方式传递有关选择的信息。下面是一个示例:
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)") })
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1533 次 |
| 最近记录: |