NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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

   

You used it in your ContentView try

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

   

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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

Reply to this topic…

You need to create an account or log in to reply.

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.