Post

Replies

Boosts

Views

Activity

Why does this view load twice?
Could someone please paste this ContentView.swift in a new SwiftUI project and run it a couple of times to see if you get the “TvAiringTodayView” message 2 times in the console when you click on the “TV” tab the first time? I have isolated it to this setup on my machine and see the following in my console when clicking the “TV” tab in the simulator:initMainViewinitMovieMainViewinitTvMainViewinitTvButtonBarViewinitTvAiringTodayViewinitTvAiringTodayViewIt 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") } }
5
0
4.1k
Apr ’20