Saturday, December 7, 2024
HomeiOS Developmentios - The way to navigate to a View in SwiftUI with...

ios – The way to navigate to a View in SwiftUI with out NavigationLink

[ad_1]

I’m new to SwiftUI and wish to navigate to a “residence” view in SwiftUI from another view. I’ve created a house button that’s added to the navigation bar on a number of views. The house button returns to the “residence” view however the navigation hyperlinks do not navigate accurately and show the unsuitable information.

I’ve learn by way of a number of solutions from others having the same downside however the solutions are not less than two years outdated and SwiftUI has modified fairly a bit since then. I’ve tried to implement most of the solutions however all the time bumped into different issues. Some performance has been deprecated and I am in search of a extra ahead wanting resolution.

Right here is a few code that reveals the issue. On this instance, I am utilizing the isActive parameter in NavigationLink to set off the house button however this appears to interrupt the NavigationLink. I’ve additionally tried utilizing the tag and choice parameters and get the identical outcomes. From studying different responses, I imagine utilizing the ForEach assertion in “ProductFamilyView” is the issue with referencing the unsuitable information. However, on this app, I want to make use of the ForEach assertion to show the product households.

If I attempt calling ProductFamilyView(), nothing occurs. On this instance, I need to navigate again to the “ProductFamilyView”. Is there one other strategy to navigate to this view with out utilizing NavigationLink?

import SwiftUI
import Mix

class Navigation: ObservableObject {
    @Printed var productFamilyIsActive : Bool = false
}

struct ContentView: View {
    
    @StateObject var navigation = Navigation()
    
    var physique: some View {
        NavigationView {
            VStack {
                LoginButtonView(buttonText: "Login")
            }
        }
        .environmentObject(navigation)
        .navigationViewStyle(StackNavigationViewStyle() )
    }
}
struct LoginButtonView: View {
    
    @State personal var navigated : Bool = false
    var buttonText : String
    
    var physique: some View {
        Button(motion: {
            self.navigated.toggle()
        }, label: {
            Textual content("(buttonText)")
        })
        .navigationBarBackButtonHidden(true)
        NavigationLink(vacation spot: ProductFamilyView(), isActive: $navigated ) {
            EmptyView()
        }
    }
}
struct ProductFamilyView: View { //That is my residence view
    
    personal var productFamilies = ProductFamilyViewModel.loadData()
    
    var physique: some View {
        Listing {
            ForEach(productFamilies) { (product) in
                ProductFamilyRow(productFamily: product.productFamily)
            }
        }
        .navigationTitle("Product Households")
        .navigationBarTitleDisplayMode(.inline)
        .navigationBarBackButtonHidden(true)
    }
}
struct ProductFamilyRow: View {
    
    @EnvironmentObject var navigation : Navigation
    @State var productFamily : String

    var physique: some View {
        NavigationLink(vacation spot: PartNumberView(productFamily: productFamily), isActive: $navigation.productFamilyIsActive) {
            Textual content("(productFamily)")
        }
        .isDetailLink(false)
    }
}
struct PartNumberView: View {
    
    @State var productFamily: String
    
    var productDetails = ProductViewModel.loadData()
        
    var physique: some View {
        Listing {
            ForEach(productDetails) { (productDetail) in
                if productFamily == productDetail.productFamily {
                    NavigationLink(vacation spot: PartNumberRow(productDetail: productDetail)) {
                        Textual content("(productDetail.partNumber)")
                    }
                }
            }
        }
        .navigationTitle("(productFamily)")
        .navigationBarItems(trailing: HomeButtonView() )
    }
}
struct PartNumberRow: View {
    
    @State var productDetail : ProductViewModel
    
    var physique: some View {
        Listing {
            Textual content("Description: (productDetail.description)")
            NavigationLink(vacation spot: ColorView(colorOptions: productDetail.colorOptions)) {
                Textual content("Coloration Choices")
            }
        }
        .navigationTitle("(productDetail.partNumber)")
        .navigationBarItems(trailing: HomeButtonView() )
    }
}
struct ColorView: View {
    
    @State var colorOptions : [Item]
    
    var physique: some View {
        Listing {
            ForEach(colorOptions) { (colour) in
                Textual content("(colour.identify)")
            }
        }
        .navigationBarItems(trailing: HomeButtonView() )
    }
}
struct HomeButtonView: View {
    
    @EnvironmentObject var navigation : Navigation
    
    var physique: some View {
        
        Button(motion: {
            self.navigation.productFamilyIsActive.toggle()
        }, label: {
            Picture(systemName: "home")
        })
        .environmentObject(navigation)
    }
}
class ProductFamilyViewModel: ObservableObject, Identifiable {
    
    var id = UUID().uuidString
    var productFamily : String = ""
    
    init(productFamily: String) {
        self.productFamily = productFamily
    }
    static func loadData() -> [ProductFamilyViewModel] {
        
        let merchandise = ["Product Family 1", "Product Family 2", "Product Family 3"]
        var productFamilies : [ProductFamilyViewModel] = []
        
        for product in merchandise {
            productFamilies.append(ProductFamilyViewModel(productFamily: product))
        }
        return productFamilies
    }
}
struct Merchandise: Identifiable, Codable {

    var id : String {
        self.identify
    }
    var identify : String
}
struct Product: Identifiable {
    
    var id = UUID().uuidString
    var partNumber    : String = "half quantity"
    var productFamily : String = "product household"
    var description   : String = "description"
    var colorOptions  : [Item] = []
}
class ProductViewModel: ObservableObject, Identifiable {
    
    var id = UUID().uuidString
    var partNumber    : String
    var productFamily : String
    var description   : String
    var colorOptions  : [Item]
    
    init(mannequin: Product) {
        self.partNumber    = mannequin.partNumber
        self.productFamily = mannequin.productFamily
        self.description   = mannequin.description
        self.colorOptions  = mannequin.colorOptions
    }
    static func loadData() -> [ProductViewModel] {
        
        let colours = [Item(name: "red"), Item(name: "white"), Item(name: "blue")]
        
        let productDetails : [ProductViewModel] = [
            ProductViewModel( model: Product(
                partNumber: "100-1111-101",
                productFamily: "Product Family 1",
                description: "Part Number 100-1111-101",
                colorOptions: colors)
            ),
            ProductViewModel( model: Product(
                partNumber: "100-1111-102",
                productFamily: "Product Family 1",
                description: "Part Number 100-1111-102",
                colorOptions: colors)
            ),
            ProductViewModel( model: Product(
                partNumber: "100-1111-103",
                productFamily: "Product Family 1",
                description: "Part Number 100-1111-103",
                colorOptions: colors)
            ),
            ProductViewModel( model: Product(
                partNumber: "200-1111-101",
                productFamily: "Product Family 2",
                description: "Part Number 200-1111-101",
                colorOptions: colors)
            ),
            ProductViewModel( model: Product(
                partNumber: "200-1111-102",
                productFamily: "Product Family 2",
                description: "Part Number 200-1111-102",
                colorOptions: colors)
            ),
            ProductViewModel( model: Product(
                partNumber: "200-1111-103",
                productFamily: "Product Family 2",
                description: "Part Number 200-1111-103",
                colorOptions: colors)
            ),
            ProductViewModel( model: Product(
                partNumber: "300-1111-101",
                productFamily: "Product Family 3",
                description: "Part Number 300-1111-101",
                colorOptions: colors)
            ),
            ProductViewModel( model: Product(
                partNumber: "300-1111-102",
                productFamily: "Product Family 3",
                description: "Part Number 300-1111-102",
                colorOptions: colors)
            ),
            ProductViewModel( model: Product(
                partNumber: "300-1111-103",
                productFamily: "Product Family 3",
                description: "Part Number 300-1111-103",
                colorOptions: colors)
            )
        ]
        return productDetails
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
            .environmentObject(Navigation())
    }
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments