首页 > 解决方案 > Swift:视图在实际设备上自行消失

问题描述

我有两个视图(AnimalDetailView 和 ReportView),它们在点击 MapView 上的按钮和注释时出现。我通过导航链接浏览视图。

当我在模拟器上运行应用程序时没有问题。但是,当我在我的实际设备上运行它时,DetailsView 和 ReportView 都会在出现后立即消失。

这背后的原因是什么?

这就是我的 NavigationView 在 MapView 上的设置方式。

ZStack {
            // NAVIGATION LINK FOR ANIMAL DETAIL VIEW
            NavigationLink(destination: AnimalDetailView(animalAnnotation: animalAnnotation), tag: "animal_detail_view_tag", selection: $annotaionSelection) { EmptyView()
            }
            
            MapModel().onAnnotationTapped(perform: { annotation in
                
                // HERE YOU GET ANNOTATION TAPPED CALLBACKS
                if annotation is AnimalAnnotation {
                    self.animalAnnotation = annotation as? AnimalAnnotation
                    annotaionSelection = "animal_detail_view_tag"
                }else if annotation is ShelterAnnotation {
                    self.shelterAnnotation = annotation as? ShelterAnnotation
                    showingAlert = true
                }
                
            }).edgesIgnoringSafeArea(.all)
            
            .alert(isPresented: $showingAlert) {
                Alert(title: Text(self.shelterAnnotation?.title ?? ""),
                      message: Text("Veteriner veya barınağı arayarak yaralı hayvanın durumunu bildir."),
                      primaryButton: .default(Text("Barınağı ara"),
                                              action: {callNumber(phoneNumber: "\(self.shelterAnnotation?.number ?? 0)" )}),
                      secondaryButton: .cancel(Text("İptal")))
            }
            
            
            NavigationLink(
                destination: ReportView(),
                label: {
                    ZStack {
                        RoundedRectangle(cornerRadius: 5)
                            .frame(width: 300, height: 60, alignment: .center)
                            .foregroundColor(Color("AnbulanceBlue"))
                        Text("YARALI HAYVAN BİLDİR")
                            .foregroundColor(.white)
                            .font(.system(size: 20))
                            .fontWeight(.bold)
                    }
                }).offset(y: 260)
        }
        .navigationBarBackButtonHidden(true)
        .toolbar {
            ToolbarItem(placement: .navigationBarTrailing) {
                Button("Çıkış yap") {
                    print("çıkış yap")
                    try! Auth.auth().signOut()
                    UserDefaults.standard.set(false,forKey: "status")
                    NotificationCenter.default.post(name: NSNotification.Name("status"), object: nil)
                    
                }
            }
        }.navigationBarHidden(false)

NavigationView 在启动的第一个屏幕上启动,如下所示:

ZStack {
            NavigationView {
                
                VStack {
                    
                    if self.status {
                        
                        MapView()
                        
                    } else {
                           
                        VStack {
                            Image("AnbulanceLogo")
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .padding(.horizontal, 50.0)
                                .edgesIgnoringSafeArea(.all)
                            Text("Anbulance")
                                .fontWeight(.heavy)
                                .foregroundColor(Color("AnbulanceBlue"))
                                .font(.system(size: 40))
                                .padding(.bottom, 100.0)
                            NavigationLink(
                                destination: LoginView(),
                                label: {
                                    ZStack {
                                        RoundedRectangle(cornerRadius: 5)
                                            .frame(width: 250, height: 50, alignment: .center)
                                            .foregroundColor(.gray)
                                            .padding()
                                        Text("Giriş yap")
                                            .foregroundColor(.white)
                                            .font(.system(size: 20))
                                            .fontWeight(.bold)
                                    }
                                })
                            NavigationLink(
                                destination: SignUpView(),
                                label: {
                                    ZStack {
                                        RoundedRectangle(cornerRadius: 5)
                                            .frame(width: 250, height: 50, alignment: .center)
                                            .foregroundColor(Color("AnbulanceBlue"))
                                        Text("Kaydol")
                                            .foregroundColor(.white)
                                            .font(.system(size: 20))
                                            .fontWeight(.bold)
                                    }
                                })
                        }
                        
                    }
                }
                .onAppear {
                    NotificationCenter.default.addObserver(forName: NSNotification.Name("status"), object: nil, queue: .main) { (_) in
                        self.status = UserDefaults.standard.value(forKey: "status") as? Bool ?? false
                        
                    }
                }

标签: swiftfirebaseswiftuimapkit

解决方案


推荐阅读