SwiftUI punto de Vista de la empujó y salió inmediatamente después del cierre de la hoja

0

Pregunta

Estoy teniendo este problema con una hoja que muestra una lista de filas, de manera que cuando una fila es presionado, la aplicación debe ir a otro punto de vista/de la pantalla (C) y la hoja se cierra, lo que está pasando, pero ver/pantalla apareció justo después de ser empujado.

iOS 15

Ver:

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()
}
}

Vista 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.: Ver C es sólo otro punto de vista, y ya he añadido el navigationViewStyle(StackNavigationViewStyle()) de la propiedad a la vista denavegación en la raíz del archivo.

ios ios15 iphone swiftui
2021-11-23 05:33:04
1

Mejor respuesta

1

Cambiar el segundo estado después de un rato a dar una oportunidad para acabar con la hoja de cierre, como

Button(action: {
    closeView = false
    print("Epale Debug: showChatValue: \(showChatView)")

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
       showChatView = true      // << here !!
    }

}, label: {
    Text("Toggle")
})
2021-11-23 06:01:23

karthikeyan

Funciona como un encanto.
Jose Ricardo Citerio Alcala

En otros idiomas

Esta página está en otros idiomas

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Slovenský
..................................................................................................................