I'm having this issue with a sheet that shows a list of rows, so when a row is pressed the app should go to another view/screen (C view) and the sheet is closed, which is happening but the view/screen is popped out right after being pushed.
iOS 15
View 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()
}
}
View 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.: View C is just another view, and I already added the navigationViewStyle(StackNavigationViewStyle()) property to the NavigationView in the root file.
CodePudding user response:
Change second state after a while to give a chance to finish sheet closing, like
Button(action: {
closeView = false
print("Epale Debug: showChatValue: \(showChatView)")
DispatchQueue.main.asyncAfter(deadline: .now() 0.3) {
showChatView = true // << here !!
}
}, label: {
Text("Toggle")
})