less than 1 minute read

Continuing on from a prior post on SwiftUI and Bindable State, I’ll add a few declarative techniques for driving the display of views based on user action

In the prior post I build a simple sign on screen where the login button enables only when something is entered into the password securetext field.

In this post I’ll demonstrate a common use case of showing the user the secure text password as clear text by tapping a button. In addition an info banner will update as well.

There are 2 common ways of displaying different views based on actions in SwiftUI. Each checks for a @State variable (binding) change.

In essence it’ll be completely declarative for the first example and lower level using a ViewBuilder technique.

Declarative version:


if showPassword {
  ShowPasswordSecurityReminder<Text>()
} else {
   ShowInfoBanner<Text>()
}

View Builder version:

ViewBuilder.buildBlock(
  showPassword == true ?ViewBuilder.buildEither(first:ShowPasswordSecurityReminder<Text>()) : ViewBuilder.buildEither(second: ShowInfoBanner<Text>())
)

Nice to have two options, however I prefer Declarative in this context.

Full Xcode project can be retrieved here

Happy Coding!