Why does TvAiringTodayView init 2 times when "TV" is clicked on the tab bar? Sometimes it only loads once, but mostly happens every time. I get the following output when I run the app and click on the "TV" tab bar button:
initMainView
initMovieMainView
initTvMainView
initTvButtonBarView
initTvAiringTodayView
initTvAiringTodayView
You may have to try it twice to see it happen. It doesn't seem like the TvAiringTodayView should init twice when clicked, but it does.
import SwiftUI
struct ContentView: View {
var body: some View {
TabView {
//Movies View
MovieMainView()
.tabItem {
Image(systemName: "film")
Text("Movies")
}
//TV View
TvMainView()
.tabItem {
Image(systemName: "tv")
Text("TV")
}
}
}
init() {
print("initMainView")
}
}
struct MovieMainView: View {
var body: some View {
VStack {
Text("MovieMainView")
}
}
init() {
print("initMovieMainView")
}
}
struct TvMainView: View {
var body: some View {
VStack {
ScrollView(.vertical, showsIndicators: false) {
TvButtonBarView()
}
}
}
init() {
print("initTvMainView")
}
}
struct TvButtonBarView: View {
@State private var selectedViewType = 0
var viewType = ["On The Air", "Top Rated", "Popular"]
var body: some View {
VStack {
Picker(selection: $selectedViewType, label: Text("Strength")) {
ForEach(0 ..< viewType.count ) {
Text(self.viewType[$0])
}
}.pickerStyle(SegmentedPickerStyle())
if selectedViewType == 0 {
TvAiringTodayView()
}
// if selectedViewType == 1 {
// TvTopRatedView()
// }
// if selectedViewType == 2 {
// TvPopularView()
// }
}
}
init() {
print("initTvButtonBarView")
}
}
struct TvAiringTodayView: View {
var body: some View {
Text("TvAiringTodayView")
}
init() {
print("initTvAiringTodayView")
}
}