ios – Counter reset to zero once I change the View swiftui

0
5


I’ve a viewModel that incorporates a modifier and func, the modifier is the full and the func calculate the full:

class TotalViewModel: ObservableObject {

@Revealed var whole: Double = 0

func whole(num: Double){

    self.whole += num

}

And there may be view that calls this func right here is it :

struct TotalView: View {

@StateObject var totalViewModel = TotalViewModel()

var physique: some View {

// TextField("", textual content: $num)

 Button(motion: {
  totalViewModel.whole(num: num)

}
}
}

That is simply easy instance from my precise code, and it work good ! it calculate superb however the issue that once I transfer to different view then come again to TotalView the full modifications and reset to 0 and it begin rely from 0

Instance :

I am within the TotalView and I press the button :
for instance num = 3 , whole = 3, and I did not change the view and num = 4 then I press button the full= 7 .
however once I change to different view and return to TotalView and num = 2 the full will equal 2.

how can I make the full proceed counting ?

Do I must make the totalViewModel an EnvironmentObject ? or there may be higher resolution ?

Thanks to your time !

LEAVE A REPLY

Please enter your comment!
Please enter your name here