ios – Push view controller from List in SwiftUI


I am in learning phase of SwiftUI

during which I have created one tab view which has 4 tabs, out of which on last tab, there is list which has following array

@State private var listItems: [NavItem] = [NavItem(id: "1", text: "Profile"),
                                                  NavItem(id: "2", text: "Leave"),
                                                  NavItem(id: "3", text: "How to use"),
                                                  NavItem(id: "4", text: "About Us"),
                                               NavItem(id: "5", text: "Logout")]

Using LIST I am able to display the same in view correctly, only problem I am facing is I am not able to push the new on selection of any item

            NavigationView {
                List (selection: $selection) {
                            ForEach(listItems, id:\.id) { item in
                                Button(action: {
                                    if(item.id == "1"){
                                        print("navigate to ----> " + item.text)
                                        NavigationLink(destination: ProfileView()) {
                                            Text("Show Detail View")
                                        }
                                    }
                                }) {
                                    Text(item.text)
                                        .foregroundStyle(.black)
                                }
                                .padding(10)
                                .listRowInsets(.init(top: 0,leading: -5, bottom: 0, trailing: -5))
                                .frame(minWidth: 111, idealWidth: .infinity, maxWidth: .infinity, alignment: .leading)
                                .tag(item.id)
                                .background(Color.clear)
                            }
                        }
            }

When I add navigationLink inside LIST, it gives me warning like below,

Result of 'NavigationLink<Label, Destination>' initializer is unused

I am able to print on console, but my list is not navigating to Profileview.

Can anyone will please help me to understand what I am doing wrong.
Any help will be appreciated.

Thanks in advance.

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img