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

How to stop showing Detail view when item in Master view deleted?

Forums > SwiftUI

Hello, hope you doing well!

I feel like I'm missing something very obvious here.

I have a Master and Detail views. User can tap on a item in Master view and details of that item is shown in Detail view. Also user can delete items from Master view — in that case, Detail view should display a text "Please select an item.".

My problem is that when I delete item in Master view on iPadOS in landscape mode, Detail view still shows content from that deleted item.

My goal is to show text "Please select an item." as soon as item deleted. For example, if "Item 2" is selected and being deleted, display only Text("Please select an item.") view after delete operation. Would appreciate any suggestions, thanks!

struct DetailView: View {
    var item: String
    var body: some View {
        Text("Detail of \(item)")
    }
}

struct ContentView: View {
    @State private var items = ["Item 1", "Item 2", "Item 3"]

    var body: some View {
        NavigationView {
            List {
                ForEach(items, id: \.self) { item in
                    NavigationLink(destination: DetailView(item: item)) {
                        Text(item)
                    }
                }
                .onDelete(perform: delete)
            }
            Text("Please select an item.")
        }
    }

    func delete(at offsets: IndexSet) {
        items.remove(atOffsets: offsets)
    }
}

4      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.