【问题标题】:SwiftUI View being pushed and popped out immediately after closing sheet关闭工作表后立即推送和弹出 SwiftUI 视图
【发布时间】:2022-01-01 15:34:03
【问题描述】:

我在显示行列表的工作表上遇到了这个问题,因此当按下一行时,应用程序应该转到另一个视图/屏幕(C 视图)并且工作表已关闭,这种情况正在发生,但视图/screen被推送后立即弹出。

iOS 15

查看 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 只是另一个视图,我已经在根文件的 NavigationView 中添加了 navigationViewStyle(StackNavigationViewStyle()) 属性。

【问题讨论】:

    标签: ios iphone xcode swiftui ios15


    【解决方案1】:

    一段时间后更改第二个状态以有机会完成工作表关闭,例如

    Button(action: {
        closeView = false
        print("Epale Debug: showChatValue: \(showChatView)")
    
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
           showChatView = true      // << here !!
        }
    
    }, label: {
        Text("Toggle")
    })
    

    【讨论】:

    猜你喜欢
    • 2021-04-10
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 2020-05-23
    • 2023-01-12
    • 2010-10-31
    相关资源
    最近更新 更多