【问题标题】:Cant get my sheet to display the right information无法让我的工作表显示正确的信息
【发布时间】:2021-03-17 23:23:18
【问题描述】:
/*

我的 MapAnnottions 上有一个 .onTapGesture。如果我第一次按 MapAnnotations 将出现黑色/空白 DisplayBusinessSheet。我希望它显示某种类型的 信息。如果我关闭工作表并按相同的注释,我只会得到黑色/空白 床单。如果我再次关闭工作表并在我的地图中按另一个位置/ MapAnnotation 它 显示正确的信息。我真的yyyyy不知道发生了什么,被solongggg卡住了,请帮助我

*/

import Foundation
import SwiftUI
import MapKit
import Firebase
import FirebaseFirestoreSwift


struct MapView: View {
    @EnvironmentObject var firebaseModel: FirebaseModel
    @EnvironmentObject var userData: UserData
    
    @State var region: MKCoordinateRegion
    @State var businessSheetPresented = false
    @State var pressedLocation: Location? = nil
    @State var pressedUser: User? = nil
    
    var locationModel = LocationModel()
    
    var body: some View {
        
        VStack{
            if let firebaseListOfLocations = firebaseModel.listOfLocations {
                Map(coordinateRegion: $region,
                    showsUserLocation: true,
                    annotationItems: firebaseListOfLocations) { location in
                    
                    //Every place has a marker
                    //anchorPoint is where we attatch the coordinates to the annotation
                    MapAnnotation(coordinate: location.userLocation!.coordinate, anchorPoint: CGPoint(x: 0.5, y: 0.5)) {
                        Image(systemName: "mappin")
                            .resizable()
                            .foregroundColor(ColorManager.darkPink)
                            .frame(width: 11, height: 30)
                            .onTapGesture(count: 1, perform: {
                                self.pressedLocation = location.userLocation!
                                self.pressedUser = location
                                
                                print("Location name: \(location.userLocation!.id)")
                                print("Pressed Location name: \(pressedLocation)")
                                print("Pressed USer Name \(pressedUser!.name)")
                                print("Pressed USer Name \(pressedUser!)")
                                
                                
                                businessSheetPresented = true
                            })
                        
                    }
                }.ignoresSafeArea()
            }
        }.sheet(isPresented: $businessSheetPresented) {
            if let pressedUser = pressedUser {
                DisplayBusinessSheet(user: pressedUser)
            }
        }
        .onAppear{
            locationModel.askForPermission()
            addUserCollectionListener()
            print("LOCATIONS: \(firebaseModel.listOfLocations!.count)")
            
        }
        
    }
    
    private func addUserCollectionListener(){
        
        if let currentUserData = userData.userDocRef {
            
            currentUserData.addSnapshotListener{ documentSnapshot, error in
                guard let document = documentSnapshot else {
                    print("Error fetching document: \(error!)")
                    return
                }
                guard let data = document.data() else {
                    print("Document data was empty.")
                    return
                }
                try! self.userData.currUserData = document.data(as: UserDataModel.self)
            }
        }
    }
  
}


//Here is the sheet I'm trying to present 
import Foundation
import SwiftUI

struct DisplayBusinessSheet: View {
    
    @State var user: User
    
    private let certefiedTitle = "Certifierad"
    private let myProductsTitle = "Mina Produkter"
    private let aboutMeTitle = "Lite om mig"
    private let socialMediaTitle = "Instagram"
    
    let notYetPostedInfo = "Information is under cunstroction"
    let heightFourth = UIScreen.main.bounds.height/4
    
    
    var body: some View {
        
        ZStack {
            VStack(alignment: .leading){
                ScrollView {
                    
                    Image("nailimage")
                        .resizable()
                        .frame(height: heightFourth)
                        .ignoresSafeArea(edges: .top)

                    
                    if let businessUser = user.businessUser {
                        Text("\(user.name)")
                            .foregroundColor(ColorManager.darkPink)
                            .fontWeight(.bold)
                            .font(.system(size: 20))
                            .padding()

                        TitleText(
                            title: certefiedTitle, textImage: Image(systemName: "link"),
                            textContent: businessUser.certifiedIn)
                            .padding(.bottom, 4)
                        
                        TitleText(
                            title: myProductsTitle, textImage: Image(systemName: "wand.and.stars"),
                            textContent: businessUser.productType)
                            .padding(.bottom, 4)
                        
                        TitleText(
                            title: aboutMeTitle, textImage: Image(systemName: "heart.text.square"),
                            textContent: businessUser.aboutMe)
                            .padding(.bottom, 4)
                        
                        TitleText(
                            title: socialMediaTitle, textImage: Image(systemName: "link"),
                            textContent: businessUser.socialMedia)
                            .padding(.bottom, 4)
                    }
                    
                }.frame(height: heightFourth * 3)
            }.onAppear{
                print("Display Business User Name: \(user.name)")
                print("Display Business User Certefied: \(user.businessUser!.certifiedIn)")
                print("Display Business User Product type: \(user.businessUser!.productType)")
                print("Display Business User About me: \(user.businessUser!.aboutMe)")
                print("Display Business User Social media: \(user.businessUser!.socialMedia)")
            }
        }
    }
}

【问题讨论】:

  • 这能回答你的问题吗? iOS14 introducing errors with @State bindings
  • 不,它没有:/我尝试使用全屏而不是工作表,但我遇到了同样的问题,不知道是工作表还是我的 onTapGesture 有问题

标签: swift swiftui


【解决方案1】:

在 iOS 14 中,sheet(isPresented:content:) 现在是预先创建的,当$businessSheetPresented 更改时视图不会刷新。

而不是.sheet(isPresented:content:)

.sheet(isPresented: $businessSheetPresented) {
    if let pressedUser = pressedUser {
        DisplayBusinessSheet(user: pressedUser)
    }
}

你应该使用.sheet(item:content:)

.sheet(item: $pressedUser) { user in
    DisplayBusinessSheet(user: pressedUser)
}

注意:使用上述解决方案,您实际上并不需要 businessSheetPresented,除非它在触发工作表之外的其他地方使用。

【讨论】:

    猜你喜欢
    • 2021-05-05
    • 2016-03-28
    • 1970-01-01
    • 1970-01-01
    • 2022-07-11
    • 2021-02-24
    • 1970-01-01
    相关资源
    最近更新 更多