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

Editing/Deleting items from a list in a sheet view

Forums > SwiftUI

I've got a bit of a tricky situation here and I'm not having much luck finding a tutorial for it.

I'm creating an audiobook library manager app. I have a fairly standing 2-column NavigationView list/detail setup.

The detail view is a FlipCard view where the front of the card is the book cover and general title/author information, and the back of the card is the description/summary of the book and more obscure information (like the inside of a dust jacket or back cover of a paperback.)

image

So, say the user is looking at a book in the detail view. At the bottom of that detail view is a toolbar with buttons allowing the user to do things like navigate chapters or edit the book details. If the user taps the button to edit the book details, it presents a sheet that contains a tab view for general details, authors, narrators, etc.

This view has a top bar that shows in all tabs that allows the user to cancel or save their editing:

    var body: some View {
        VStack {
            topBar
            TabView(selection: $selection,
                    content:  {
                        generalTab
                            .tabItem { Text("General") }
                            .tag(Tab.general)
                        authorsTab
                            .tabItem { Text("Authors") }
                            .tag(Tab.authors)
                        narratorsTab
                            .tabItem { Text("Narrators") }
                            .tag(Tab.narrators)
                        genresTab
                            .tabItem { Text("Gebres") }
                            .tag(Tab.genres)
                        summaryTab
                            .tabItem { Text("Summary") }
                            .tag(Tab.description)
                    })
        }
    }

private var topBar: some View {
        HStack {
            Button(
                action: { cancel() },
                label: {
                    Text("Cancel")
                        .font(.title3)
                        .bold()
                })
            Spacer()
            Text(title)
            Spacer()
            Button(
                action: { save() },
                label: {
                    Text("Save")
                        .font(.title3)
                        .bold()
                })
        }.padding()
    }

image

image

(layouts aren't fine-tuned yet, I'm working on functionality for the moment.)

I want to add the ability to add/remove/re-order the authors, but I'm not in a NavigationView, I'm in a tabView that is inside a modal/sheet view. I don't think I can activate EditMode in this view, can I? (or if so, nothing I've tried so far is correct.)

How would I go about synthesizing EditMode behavior for lists inside a sheet view?

    @State private var authorEditing = EditMode.inactive
    private var authorsTab: some View {
        VStack(alignment: .leading) {
            HStack {
                Text("Authors")
                    .foregroundColor(.gray)
                    .font(.title2)
            }.padding()

            List {
                ForEach(authors) {
                    LineRowView($0)
                }
            }
            .listStyle(InsetListStyle())
            .onAppear(perform: {
                authors = book.authors
            })
        }
    }

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out 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.