[英]SwiftUI, weird NavigationLink behavior when working with actionSheet
我想在讓他/她進入之前先檢測用戶是否滿足先決條件。如果不滿足先決條件,應用程序將彈出一個 actionSheet 並向用戶展示一些解鎖該功能的方法。 當我點擊文本時,它工作得很好。 但是當我點擊列表中的空白處時。 它只是跳過綁定。 奇怪的是,在我的實際項目中,綁定變為“真”,即使我只將它設置為假。 這是問題。 我是在使用正確的方法還是錯過了什么? 或者這是一個錯誤? 謝謝你。
struct ContentView: View {
@State var linkOne = false
@State var linkTwo = false
@State var linkThree = false
@State var actionOne = false
@State var actionTwo = false
@State var actionThree = false
var body: some View {
NavigationView{
List{
NavigationLink("Destination View One", destination: DestOneView(), isActive: self.$linkOne)
.actionSheet(isPresented: self.$actionOne) { () -> ActionSheet in
ActionSheet(title: Text("Hello"), message:Text("This is weird"), buttons: [ActionSheet.Button.cancel()])
}.onTapGesture {
self.actionOne = true
// self.linkOne = true
}
NavigationLink("Destination View Two", destination: DestTwoView(), isActive: self.$linkTwo)
.actionSheet(isPresented: self.$actionTwo) { () -> ActionSheet in
ActionSheet(title: Text("Hello"), message:Text("This is weird"), buttons: [ActionSheet.Button.cancel()])
}
.onTapGesture {
self.actionTwo = true
// self.linkTwo = true
}
NavigationLink("Destination View Three", destination: DestThreeView(), isActive: self.$linkThree)
.actionSheet(isPresented: self.$actionThree) { () -> ActionSheet in
ActionSheet(title: Text("Hello"), message:Text("This is weird"), buttons: [ActionSheet.Button.cancel()])
}
.onTapGesture {
self.actionThree = true
// self.linkThree = true
}
}
}
}
}
其他三種觀點。
struct DestOneView: View {
var body: some View {
Text("First View")
}
}
struct DestTwoView: View {
var body: some View {
Text("Second View")
}
}
struct DestThreeView: View {
var body: some View {
Text("Third View")
}
}
通常,覆蓋手勢在List
中效果不佳。 解決方案之一是使用Button
來呈現NavigationLink
:
import SwiftUI
struct ContentView: View {
@State private var linkOne = false
...
var body: some View {
NavigationView {
List {
NavigationLink(destination: SomeView(), isActive: $linkOne) {
EmptyView()
}
Button(action: {
// here you can perform actions
self.linkOne = true
}, label: {
Text("Some text!")
})
Spacer()
}
}
}
}
當我回來測試我的代碼時。 解決方案並沒有真正奏效。 可能是因為列表的錯誤。 另一個帖子上的人說,如果工作表在列表中,新視圖中的列表只會顯示一次。 因此,當我點擊 Xcode 版本 11.5 中的按鈕時,我在新視圖中只得到一個空列表。 出於某種原因,如果我使用 NavigationView,所有內容都會縮小到視圖的中間,而不是與頂部對齊。
我的解決方法是在.onAppear 中設置綁定。 它會在視圖加載時彈出 actionSheet。 然后使用presentationMode方法返回上一個視圖。
@Environment(\.presentationMode) var presentationMode
.
.
.
ActionSheet.Button.default(Text("Dismiss"), action: {
self.presentationMode.wrappedValue.dismiss()}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.