skip to Main Content

I am trying to call a detailed view of an item when an item is tapped. In this case, the item is a pair of trousers in the MarketplaceTrouserView. When I call TrouserDetailView, I get an error. This must be to do with initialising but I’ve repeatedly tried this and failed. What could be the solution?

MarketplaceTrouserView:

import SwiftUI

struct MarketplaceTrouserView: View {
   
    @StateObject var MarketplaceModel = MarketplaceViewModel()
    @State private var selectedMarketplaceFilter: MarketplaceFilterViewModel = .trouser
    @State var showDetailTrouser = false
    @State var selectedTrouser : Trouser!
    @EnvironmentObject var sharedData: SharedDataModel
    
    var body: some View {
        
        var columns = Array(repeating: GridItem(.flexible()), count: 2)
        
        ZStack{  
            VStack{        
                HStack {
                    
                    Text("Find Trousers To Buy")
                }
                    }
            }                
                if MarketplaceModel.trousers.isEmpty{
                    
                    ProgressView()
                }
                else{
                    
                    ScrollView {

                        LazyVGrid(columns: Array(repeating: GridItem(.flexible(),spacing: 10), count: 2),spacing: 20){
                            
                            ForEach(MarketplaceModel.filteredTrouser){trouser in
                                
                                // Trouser items in grid view 
                                
                                    TrouserView(trouserData: trouser)
                                    

                                        .onTapGesture {
                                            
                                            withAnimation {
 
                                                selectedTrouser = trouser
                                                showDetailTrouser.toggle()
                                            }
                                        }
                            }
                        }
                    }
                }
            }
            
            if selectedTrouser != nil && showDetailTrouser{
                
                TrouserDetailView(/*Here is the error asking for trouserData & showDetailTrouser*/)
                
            }
       }
}

}

TrouserDetailView:

import SwiftUI
import SDWebImageSwiftUI

struct TrouserDetailView: View {
    @State var trouserData : Trouser
    @State var showDetailTrouser: Bool
    @EnvironmentObject var sharedData: SharedDataModel    
    @EnvironmentObject var marketplaceData: MarketplaceViewModel
       
    
    
    var body: some View {
        
    ScrollView {
        
        VStack{
            
            HStack {
                
                Button(action: {

                                        withAnimation(.easeOut){showDetailTrouser.toggle()}

                                    }) {
                                        Image(systemName: "arrow.backward.circle.fill")

                                    }

                
                    Text(trouserData.trouser_name)
            }
            
            VStack {
                
                WebImage(url: URL(string: trouserData.trouser_image))   

            }
            
        }
    }
        }

Trouser Model:

import SwiftUI
import FirebaseFirestoreSwift
import Firebase

struct Trouser: Identifiable, Codable {
    
    @DocumentID var id: String?
    var trouser_name: String = ""
    var trouser_image: String = ""
}

The error is when i call TrouserDetailView (as marked in the code)

After applying changes, my MainPage view has errors in the initialiser.

MainPage


struct MainPage: View {

    @StateObject var appModel: AppViewModel = .init()
    
    @StateObject var sharedData: SharedDataModel = SharedDataModel()

    
    @State var shirtData : Shirt
    @State var showDetailShirt: Bool   
    @State var trouserData : Trouser
    @State var showDetailTrouser: Bool
    @Namespace var animation
    

    
    init () {
        
        @State var shirtData = Shirt()
        @State var showDetailShirt = false
        
        @State var trouserData = Trouser()
        @State var showDetailTrouser = false
       
        
        UITabBar.appearance().isHidden = true
    } //ERROR HERE: 'Return from initializer without initializing all stored properties'

    var body: some View {
        
        VStack {

            TabView(selection: $appModel.currentTab) {
                Marketplace()
                    .environmentObject(sharedData)
                    .tag(Tab.Market)
                    .setUpTab()
                
                Home()
                    .environmentObject(sharedData)
                    .tag(Tab.Home)
                    .setUpTab()
            }

2

Answers


  1. By the logic of provided code, state is not needed for the first one and second should be replaced with binding, ie.

    struct TrouserDetailView: View {
        var trouserData : Trouser
        @Binding var showDetailTrouser: Bool
    
        // ...
    

    and call

    if selectedTrouser != nil && showDetailTrouser {
        
        TrouserDetailView(trouserData: selectedTrouser, 
                    showDetailTrouser: $showDetailTrouser)
        
    }
    
    Login or Signup to reply.
  2. @State var trouserData : Business
    @State var showDetailTrouser: Bool

    These variables you must initialize the value locally or switch to @Binding to bind the data with the parent variables.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search