SwiftUI:我无法通过 @EnvironmentObject 切换 bool 值

时间:2021-03-22 23:34:25

标签: xcode swiftui

我在通过@EnvironmentObject 更改 Bool 值时遇到问题。我想通过单击第三个视图中的按钮来显示包含在第二个视图中的一个视图。我试过 environmentObject 但对我来说它不起作用。当我按下这个按钮时什么都没有发生

这是第一次查看代码(迷你播放器):

struct MiniPlayer: View {

@EnvironmentObject var test : testClass

@ObservedObject var data : NewsData
var animation : Namespace.ID
@Binding var appear : Bool
@Binding var exist : Bool

var body: some View {
    VStack{
        if !test.appearing{
            Divider().animation(.spring())
        }
        HStack(spacing: 15){
            
            if test.appearing{Spacer(minLength: 0)}
            
            AsyncImage(url: URL(string: data.nowosci.first?.image ?? url)!, placeholder: {ProgressView()})
                .aspectRatio(contentMode: .fill)
                .frame(width: test.appearing ? screenH/3 : 50, height: test.appearing ? screenH/3 : 50)
                .cornerRadius(15)
            
            if !test.appearing{
            
                Text(data.nowosci.first?.name ?? "Loading...")
                    .font(.system(size: 15))
                    .fontWeight(.bold)
            }
            
            Spacer(minLength: 0)
            
            if !test.appearing{
                Button(action: {
                    
                }, label: {
                    Image(systemName: "backward.fill")
                        .font(.title2)
                        .foregroundColor(mainCol)
                })
                Button(action: {
                    
                }, label: {
                    Image(systemName: "play.fill")
                        .font(.title2)
                        .foregroundColor(mainCol)
                })
                Button(action: {
                    
                }, label: {
                    Image(systemName: "forward.fill")
                        .font(.title2)
                        .foregroundColor(mainCol)
                })
                Button(action: {
                    exist = false
                }, label: {
                    Image(systemName: "xmark")
                        .font(.title3)
                        .foregroundColor(.black)
                }).padding(.leading, 5)
            }
        }
        .padding(.horizontal)
        if !test.appearing{
            Divider().animation(.spring())
        }
    }
    .frame(maxHeight: test.appearing ? .infinity : 70)
    .background(Color.white)
    .onTapGesture {
        withAnimation(.spring()){
            test.appearing.toggle()
        }
    }.ignoresSafeArea()
    
    .offset(y: test.appearing ? 0 : -92)
        
    .navigationBarHidden(test.appearing ? true : false)
  }
}

这是类的代码:ObservableObject:

class testClass : ObservableObject{
    @Published var appearing : Bool = false 
}

第二视图(主页)中第一视图(迷你播放器)的代码:

struct MainPage: View {

@State var exist = true
@State var appear = false
@Namespace var animation

@State private var isShowing = false
@State var seledtedIndex = 2

let tabBarImageNames = ["video.fill", "music.note", "house.fill", "calendar", "creditcard.fill"]
let tabBarTextNames = ["Wideo", "Audio", "Strona główna", "Nowości", "Wesprzyj nas"]

@ObservedObject var data : NewsData

var body: some View {
    ZStack(alignment: Alignment(horizontal: .center, vertical: .bottom)){
        
        
        Color.white.edgesIgnoringSafeArea(.all)
        
        VStack{
            ZStack{
                switch seledtedIndex{
                
                case 0:
                    NavigationView{
                        ZStack{
                            Color.white.ignoresSafeArea()
                            Video()
                        }
                        
                    }.ignoresSafeArea()
                    .navigationBarItems(leading: Button(action: {
                        withAnimation(.spring()){
                            isShowing.toggle()
                        }
                    }, label: {
                        Image(systemName: "list.dash")
                            .font(.system(size: 25))
                            .foregroundColor(.black)
                    }))
                    .navigationTitle("Video")
                    .navigationBarTitleDisplayMode(.inline)
                    
                case 1:
                    
                    NavigationView{
                        Audio()
                        
                    }.navigationBarItems(leading: Button(action: {
                        withAnimation(.spring()){
                            isShowing.toggle()
                        }
                    }, label: {
                        Image(systemName: "list.dash")
                            .font(.system(size: 25))
                            .foregroundColor(.black)
                    }))
                    .navigationTitle("Audio")
                    .navigationBarTitleDisplayMode(.inline)
                case 2:
                    NavigationView{
                        ZStack{
                            Color.white.ignoresSafeArea()
                            Strona_glowna()
                        }
                    }.navigationBarItems(leading: Button(action: {
                        withAnimation(.spring()){
                            isShowing.toggle()
                        }
                    }, label: {
                        Image(systemName: "list.dash")
                            .font(.system(size: 25))
                            .foregroundColor(.black)
                    }))
                    .navigationTitle("Strona główna")
                    .navigationBarTitleDisplayMode(.inline)
                case 3:
                    NavigationView{
                        Nowosci(data: data)
                    }.navigationBarItems(leading: Button(action: {
                        withAnimation(.spring()){
                            isShowing.toggle()
                        }
                    }, label: {
                        Image(systemName: "list.dash")
                            .font(.system(size: 25))
                            .foregroundColor(.black)
                    }))
                    .navigationTitle("Nowosci")
                    .navigationBarTitleDisplayMode(.inline)
                case 4:
                    NavigationView{
                        Wesprzyj_nas()
                    }.navigationBarItems(leading: Button(action: {
                        withAnimation(.spring()){
                            isShowing.toggle()
                        }
                    }, label: {
                        Image(systemName: "list.dash")
                            .font(.system(size: 25))
                            .foregroundColor(.black)
                    }))
                    .navigationTitle("Wesprzyj nas")
                    .navigationBarTitleDisplayMode(.inline)
                default:
                    NavigationView{
                        Strona_glowna()
                            .navigationTitle("Strona główna")
                    }.navigationBarItems(leading: Button(action: {
                        withAnimation(.spring()){
                            isShowing.toggle()
                        }
                    }, label: {
                        Image(systemName: "list.dash")
                            .font(.system(size: 25))
                            .foregroundColor(.black)
                    }))
                    .navigationTitle("Strona główna")
                    .navigationBarTitleDisplayMode(.inline)
                    
                }
            } //top bar
            
            Spacer()
            
            VStack {
                HStack{
                    ForEach(0..<5) { num in
                        Button(action: {
                            withAnimation{
                                seledtedIndex = num
                            }
                        }, label: {
                            Spacer(minLength: 0)
                            Text(tabBarImageNames[num])
                                .font(.system(size: 10))
                                .foregroundColor(seledtedIndex == num ? mainCol : Color.black.opacity(0.5))
                                .offset(y: seledtedIndex == num ? -10 : 0)
                            Spacer(minLength: 0)
                        })
                        
                    }
                    
                }.padding(.top, 2)
                
                HStack{
                    ForEach(0..<5) { num in
                        Button(action: {
                            withAnimation{
                                seledtedIndex = num
                            }
                        }, label: {
                            Spacer(minLength: 0)
                            Text(tabBarTextNames[num])
                                .font(.system(size: 10))
                                .foregroundColor(seledtedIndex == num ? mainCol : Color.black.opacity(0.5))
                                .offset(y: seledtedIndex == num ? -10 : 0)
                            Spacer(minLength: 0)
                        })
                        
                    }
                    
                }.padding(.top, 2)
            }.padding(10).padding(.bottom, 20) //bottom bar
            
        }// top and bottom bar
        
        if exist {
            MiniPlayer(data: data, animation: animation, exist: $exist).environmentObject(testClass())
         }
      }
   }
}

第三个视图代码 (strona_glowna):

struct Strona_glowna: View {

@StateObject var test = testClass()

var body: some View {
    
    NavigationView{
        VStack{
            NavigationLink(destination: TestFile(), label: {
                Text("przejdz dalej")
            }).padding()
            
            Button(action: {
                test.appearing.toggle()
            }, label: {
                Text("pokaż Mini Player")
            })
        }
        .navigationBarHidden(true)
    }
    .environmentObject(test)
   }
}

最后一个代码来自@main:

@main
struct Practise1App: App {

let newsdata = NewsData()

init() {
    FirebaseApp.configure()
    newsdata.loadData()
    UINavigationBar.appearance().barTintColor = .white
}

var body: some Scene {
    WindowGroup {
        StarterPage(data: newsdata).environmentObject(testClass())
      }
   }
}

0 个答案:

没有答案