After a helpful guiding hand setting up source control on the new module 4 project (with a challenge to do the same on the old War Card Challenge app), lesson 3 of module 4 dives right into a new ‘view’, that being “TabView”.
Having been taken through various other views in previous lessons (ScrollView, etc), TabView is a little more intuitive and, thus, more easily understandable. As with many “new” features we learn, however, it’s more a factor of remembering what modifiers and actions can be attributed to the new item.
Nevertheless, the challenge to today’s lesson, whilst initially looking a bit like a big job, came across relatively straightforward. I did need a quick refresher on how to repeat text multiple times (which “For/ForEach” looping method would be correct), I completed the challenge without much in the way of hiccups.
I must be becoming more attuned to how Swift & SwiftUI thinks (I’m sure it does think – nothing this convoluted would be incapable of thinking!).
struct ContentView: View {
@State var tabIndex = 2 // default tab to show
var body: some View {
TabView(selection: $tabIndex) { // $ means "two way binding"
Text("This is tab 1")
.tabItem {
VStack {
Image(systemName: "pencil")
Text("Tab 1")
}
}
.tag(1)
VStack {
Text("This is tab 2")
Text("This is some more text")
}
.tabItem {
VStack {
Image(systemName: "star")
Text("Tab 2")
}
}
.tag(2)
} // end TabView
}
}