TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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 Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.