UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Combination of Searchable, NavigationLink and navigationBarItems break app

Forums > SwiftUI

Hi,

When you compile below code and enter "J" in the search field and then click one of the entries, the app crashes when showing the NavigationLink destination. Clicking on an entry without any active filter works fine.

//
//  ContentView.swift
//  NavigationLinkIssue
//
//  Created by Max on 01.07.22.
//

import SwiftUI
import Combine

public class Person: Identifiable, ObservableObject {
    public var id = UUID()
    var firstName: String = ""
    var lastName: String = ""
}

struct PersonView: View{
    @ObservedObject var person: Person = Person()
    @State public var navigationBarTitle: String = "New item"

    var body: some View{
        Group{
            VStack{
                Form{
                    Section(header: Text("Some section")){
                        Text("Detail for \(person.firstName) \(person.lastName)")
                    }

                }
            }
        }//Group
        .navigationBarTitle(self.navigationBarTitle)
        .navigationBarItems(leading: (
            EmptyView()
        ), trailing: (
            Button(action: {
                print("-------SAVE-------")
            }, label: {
                Text("Save")
            }
                  )
        ))
    }
}

struct ContentView: View {
    @State var persons: [Person] = []
    @State var searchText = ""

    var searchResults: [Person] {
        if searchText.isEmpty {
            return persons
        } else {
            return persons.filter { $0.firstName
                    .lowercased()
                    .contains(searchText.lowercased())
                ||
                $0.lastName
                    .lowercased()
                    .contains(searchText.lowercased())
            }
        }
    }

    var body: some View {
        TabView{
            NavigationView{
                ZStack{
                    List{
                        ForEach(searchResults){person in
                            NavigationLink(destination: PersonView(person: person)) {
                                Text("\(person.firstName) \(person.lastName)")
                            }
                        }//ForEach
                        .listStyle(.plain)
                    }//List
                    .refreshable {
                        print("refresh triggered")
                    }
                    .searchable(text: $searchText)
                }//ZStack
                .toolbar(content: {
                    ToolbarItem{
                        Button {
                            print("button pressed")
                        } label: {
                            Image(systemName: "line.3.horizontal.decrease.circle.fill")
                        }
                    }
                })
                .navigationTitle("Persons")

            }//NavigationView
            .onAppear(){
                self.initPersons()
            }
            .tabItem {
                Image(systemName: "rectangle.on.rectangle")
                Text("Persons")
            }
        }

    }

    func initPersons(){
        let person = Person()
        person.firstName = "John"
        person.lastName = "Doe"

        let person1 = Person()
        person1.firstName = "Jane"
        person1.lastName = "Doe"

        let person2 = Person()
        person2.firstName = "Tim"
        person2.lastName = "Appleseed"

        let person3 = Person()
        person3.firstName = "Sally"
        person3.lastName = "Snickers"

        self.persons.append(person)
        self.persons.append(person1)
        self.persons.append(person2)
        self.persons.append(person3)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

I have noticed that when I remove the code block

.navigationBarItems(leading: (
            EmptyView()
        ), trailing: (
            Button(action: {
                print("-------SAVE-------")
            }, label: {
                Text("Save")
            }
                  )
        ))

from the PersonView view, it works fine. What am I missing here?

Max

2      

You used it in your ContentView try

.toolbar {
    ToolbarItem(placement: .navigationBarTrailing) {
        Button {
            print("-------SAVE-------")
        } label: {
           Text("Save")
        }
    }
}

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.