SwiftUI 视图在关闭后立即被推送并弹出 sheet
SwiftUI View being pushed and popped out immediately after closing sheet
我遇到了显示行列表的 sheet 的问题,因此当按下一行时,应用程序应该转到另一个 view/screen(C 视图)和 sheet 已关闭,这正在发生,但 view/screen 在被推入后立即弹出。
iOS15
视图A:
import SwiftUI
struct A: View {
@State private var showNewMessage = false
@State private var showChatView = false
var body: some View {
ZStack(alignment: .bottomTrailing){
NavigationLink(
destination: C(),
isActive: $showChatView,
label: {})
//NavigationLink(destination: EmptyView(), label: {})
ScrollView {
VStack(alignment: .leading) {
ForEach( 1...10, id: \.self){_ in
NavigationLink(
destination: C(),
label: {
ChatView()
})
}
}
}
Button(action: {
//showNewMessage.toggle()
showNewMessage = true
}, label: {
Image(systemName: "pencil")
.resizable()
.scaledToFit()
.frame(width: 24, height: 24)
})
.padding()
.foregroundColor(Color.white)
.background(Color.blue)
.clipShape(Circle())
.sheet(isPresented: $showNewMessage, onDismiss: test, content: {
B(showChatView: $showChatView, closeView: $showNewMessage)
}).navigationViewStyle(StackNavigationViewStyle())
}
.padding(.horizontal)
}
func test(){
print("Epale Debug: showChatValue: \(showChatView)")
}
func toggle(){
showChatView.toggle()
}
}
视图 B:
import SwiftUI
struct B: View {
@Binding var showChatView: Bool
@Binding var closeView: Bool
//@Environment(\.presentationMode) var mode
var body: some View {
Button(action: {
//showChatView.toggle()
showChatView = true
closeView = false
print("Epale Debug: showChatValue: \(showChatView)")
//mode.wrappedValue.dismiss()
}, label: {
Text("Toggle")
})
}
}
P.S.: 视图 C 只是另一个视图,我已经将 navigationViewStyle(StackNavigationViewStyle()) 属性 添加到根文件中的 NavigationView。
一段时间后更改第二个状态,以便有机会完成 sheet 关闭,例如
Button(action: {
closeView = false
print("Epale Debug: showChatValue: \(showChatView)")
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
showChatView = true // << here !!
}
}, label: {
Text("Toggle")
})
我遇到了显示行列表的 sheet 的问题,因此当按下一行时,应用程序应该转到另一个 view/screen(C 视图)和 sheet 已关闭,这正在发生,但 view/screen 在被推入后立即弹出。
iOS15
视图A:
import SwiftUI
struct A: View {
@State private var showNewMessage = false
@State private var showChatView = false
var body: some View {
ZStack(alignment: .bottomTrailing){
NavigationLink(
destination: C(),
isActive: $showChatView,
label: {})
//NavigationLink(destination: EmptyView(), label: {})
ScrollView {
VStack(alignment: .leading) {
ForEach( 1...10, id: \.self){_ in
NavigationLink(
destination: C(),
label: {
ChatView()
})
}
}
}
Button(action: {
//showNewMessage.toggle()
showNewMessage = true
}, label: {
Image(systemName: "pencil")
.resizable()
.scaledToFit()
.frame(width: 24, height: 24)
})
.padding()
.foregroundColor(Color.white)
.background(Color.blue)
.clipShape(Circle())
.sheet(isPresented: $showNewMessage, onDismiss: test, content: {
B(showChatView: $showChatView, closeView: $showNewMessage)
}).navigationViewStyle(StackNavigationViewStyle())
}
.padding(.horizontal)
}
func test(){
print("Epale Debug: showChatValue: \(showChatView)")
}
func toggle(){
showChatView.toggle()
}
}
视图 B:
import SwiftUI
struct B: View {
@Binding var showChatView: Bool
@Binding var closeView: Bool
//@Environment(\.presentationMode) var mode
var body: some View {
Button(action: {
//showChatView.toggle()
showChatView = true
closeView = false
print("Epale Debug: showChatValue: \(showChatView)")
//mode.wrappedValue.dismiss()
}, label: {
Text("Toggle")
})
}
}
P.S.: 视图 C 只是另一个视图,我已经将 navigationViewStyle(StackNavigationViewStyle()) 属性 添加到根文件中的 NavigationView。
一段时间后更改第二个状态,以便有机会完成 sheet 关闭,例如
Button(action: {
closeView = false
print("Epale Debug: showChatValue: \(showChatView)")
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
showChatView = true // << here !!
}
}, label: {
Text("Toggle")
})