0

I have ImageSettingView which Im using multiple places. When I used this view at multiple place in my project it takes static name all time in the navigationBarItem i.e Text("Image Collab View"). I want to give dynamic name change based on where parent view it comes from.

ImageColorView -> ImageSettingView (Back NavButon text - ImageColor ) ProfilePictureView -> ImageSettingView (Back NavButon text - ProfilePictureView )

Sample code of ImageSettingView :-

struct ImageSettingView : View {


 var body: some View {
        GeometryReader { geometry in
            VStack {
                Form {
                Text("Image View")
                }
                .navigationBarBackButtonHidden(true)
                .navigationBarItems(
                    leading:
                        Button(action: {
                            onUserImage()
                        }) {
                            HStack {
                                Image(systemName: "backward")
                                Text("Image")
                                // I want to use change above text based on what name of the parent view was
                            }
                        },
                    trailing:
                        Button(action: {
                            onDelete()
                        }) {
                            HStack {
                                Text("Delete")
                            }
                        }
                )
             
            }
        }
    }

Sample image of mock up enter image description here

Mai pa
  • 45
  • 5

1 Answers1

0

Since you always know what the parent view is, try this approach, passing the name of the parent view to your ImageSettingView.

 struct ImageSettingView: View {
     let parentName: String  // <-- here
     //...
     Image(systemName: "backward")
     Text(parentName)  // <-- here
     //...

and call it like this:

ImageSettingView(parentName: "TestView")

or

ImageSettingView(parentName: "ProfilePictureView")

or

ImageSettingView(parentName: "\(Self.self)")

EDIT-1: this is the test code I used to show my answer works:

struct ContentView: View {
    var body: some View {
        NavigationStack {
            VStack(spacing: 50) {
                Text("This is ContentView")
                NavigationLink("To ProfileView", destination: ProfileView(parentName: "ContentView"))
            }
        }
    }
}

struct ProfileView: View {
    let parentName: String
    var body: some View {
        VStack(spacing: 50) {
            Text("This is ProfileView")
            ImageSettingView(parentName: parentName)
            NavigationLink("To Next View ", destination: NextView(parentName: "\(Self.self)"))
        }
    }
}

struct NextView: View {
    let parentName: String
    
    var body: some View {
        VStack(spacing: 50) {
            Text("This is NextView")
            ImageSettingView(parentName: parentName)
        }
    }
}

struct ImageSettingView: View {
    @Environment(\.dismiss) var dismiss
    
    let parentName: String // <--here
    
    var body: some View {
        GeometryReader { geometry in
            VStack {
                Form {
                    Text("Image View")
                }
                .navigationBarBackButtonHidden(true)
                .navigationBarItems(
                    leading:
                        Button(action: {
                            //  onUserImage()
                            dismiss()
                        }) {
                            HStack {
                                Image(systemName: "backward")
                                Text(parentName)  // <--here
                            }
                        },
                    trailing:
                        Button(action: {
                            //   onDelete()
                        }) {
                            HStack {
                                Text("Delete")
                            }
                        }
                )
            }
        }
    }
}