NEW: Nominations are now open for the 2019 Swift Community Awards! >>

Final polish

Paul Hudson    @twostraws   

If you try using the app, you’ll soon see it has two problems:

  1. Adding an expense doesn’t dismiss AddView; it just stays there.
  2. When you add an expense, you can’t actually see any details about it.

Before we wrap up this project, let’s fix those to make the whole thing feel a little more polished.

First, dismissing AddView is done by storing a reference to the view’s presentation mode, then calling dismiss() on it when the time is right. The presentation mode is controlled by the view’s environment, and links to the isPresented parameter for our sheet – that Boolean gets set to true by us to show AddView, but will be flipped back to false by the environment when we call dismiss() on the presentation mode.

Start by adding this property to AddView:

@Environment(\.presentationMode) var presentationMode

You’ll notice we don’t specify a type for that – Swift can figure it out thanks to the @Environment property wrapper.

Next, we need to call presentationMode.wrappedValue.dismiss() when we want the view to dismiss itself. This causes the showingAddExpense Boolean in ContentView to go back to false, and hides the AddView. We already have a Save button in AddView that creates a new expense item and appends it to our existing expenses, so add this on the line directly after:

self.presentationMode.wrappedValue.dismiss()

That solves the first problem, which just leaves the second: we show the name of each expense item but nothing more. This is because the ForEach for our list is trivial:

ForEach(expenses.items) { item in
    Text(item.name)
}

We’re going to replace that with a stack within another stack, to make sure all the information looks good on screen. The inner stack will be a VStack showing the expense name and type, then around that will be a HStack with the VStack on the left, then a spacer, then the expense amount. This kind of layout is common on iOS: title and subtitle on the left, and more information on the right.

Replace the existing ForEach in ContentView with this:

ForEach(expenses.items) { item in
    HStack {
        VStack(alignment: .leading) {
            Text(item.name)
                .font(.headline)
            Text(item.type)
        }

        Spacer()
        Text("$\(item.amount)")
    }
}

Now run the program one last time and try it out – we’re done!

SAVE 20% ON iOS CONF SG The largest iOS conference in Southeast Asia is back in Singapore for the 5th time in January 2020, now with two days of workshops plus two days of talks on SwiftUI, Combine, GraphQL, and more! Save a massive 20% on your tickets by clicking on this link.

MASTER SWIFT NOW
Buy Testing Swift Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns Buy Swift Coding Challenges Buy Server-Side Swift (Vapor Edition) Buy Server-Side Swift (Kitura Edition) Buy Hacking with macOS Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with Swift Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5