2

I have a ContentView that included five TabView. enter image description here

I want to complete the function like...

User clicks the different tab of TabView every time, it should refreshing at the first time(show loadingView and initialize those variables of this TabView). After refreshing view, the data & UI of the view would be updated perfectly.

enter image description here

I tried to use the feature of declare language, trying to refresh view every time by changing the state variable(@State & @Binding). Besides, I also tried to use ".onAppear & .onDisAppear" to refresh these view. However, it didn't work.

Does anyone can give me some advice? Any help would be gratefully.

Here is my code for now:

ContentView

var body: some View {
    TabView(selection: $selection) {
        Personalization_View(isSelected: self.$selected)
            .tabItem() {
                Image(systemName: "person.crop.square")
        }.onTapGesture {
            self.selected.toggle()
        }.tag(0) //index 1
        WiFi_View()
            .tabItem {
                Image(systemName: "wifi")
        }.tag(1) //index 2
        Dashboard_View()
            .tabItem {
                Image(systemName: "house")
        }.tag(2) //index 3
        Networking_View()
            .tabItem {
                Image(systemName: "shield")
        }.tag(3) // index 4
            Management_View()
            .tabItem {
                Image(systemName: "wrench")
        }.tag(4) //index 5
    }.accentColor(.blue).padding(10)
    }.present(isPresented: $firstSetup, goView: SetupPage(isAgree: $firstSetup)).navigationBarBackButtonHidden(true).hidden()
}

Personalization_View will show a toggle that represents the status of server(On or Off). Therefore, I need to refresh view and show the correct status.

p.s. I get the status of server by connecting to device(API way).

07/22/20 Solution by @Asperi:

I remove the init() part from my ViewModel(ObservableObject) and adding the code into my Personaliztion_View .onAppear().

Personaliztion_View revising code

@ObservedObject var updater: Personalization_ViewModel = Personalization_ViewModel()
VStack {
SOME CODE HERE
    }.onAppear() {
      self.updater.updateStatus()
    }
Spencer Reid
  • 409
  • 3
  • 14
  • 1
    Your loading/updating should be managed by view model (ObservableObject) of each view, so once loaded data should remain in view model, the corresponding ObservedObject wrapper updates view automatically. – Asperi Jul 22 '20 at 07:56
  • @Asperi Thanks for your advice, I will try it. : - ) – Spencer Reid Jul 22 '20 at 07:58
  • @Asperi omg it work! I never notice this before. You just save my day. – Spencer Reid Jul 22 '20 at 08:01

0 Answers0