1

I want to trigger the update of a subview in SwiftUI by changing the state of a @State variable, since it does not update alone when I change the Wallet Object since it is defined as EnvironmentObject. The thing is that I have to initialize the view with environmentObject, and it returns Some View, and cannot cast it to WalletView as it should seem that it should.

var walletView = WalletView().environmentObject(Wallet(cards: reminders)) if walletView = walletView as? WalletView{ walletView.isPresented = !walletView.isPresented } 

How can I access the WalletView object?

I've tried:

var walletView = WalletView() let someWalletView = walletView.environmentObject(Wallet(cards: reminders)) walletView.isPresented = !walletView.isPresented 

but the walletView doesn't seem to update. Any clue?

3
  • can you add the ContentView code to be clear, what you want to achieve? Commented Feb 21, 2020 at 2:59
  • "I want to trigger the update of a subview ... since it does not update alone when I change the Wallet Object since it is defined as EnvironmentObject." ??? it could be done ... you better to solve it, casting from some View is impossible by definition. Commented Feb 21, 2020 at 8:17
  • Thanks, I solved that by changing the variable to @Binding. Commented Feb 21, 2020 at 20:29

2 Answers 2

1

The SwiftUI approach is to change view state inside view, so as far as I understood what your going to do with WalletView it could be achieved like in the following (scratchy):

struct WalletView: View { ... var body: some View { _some_internal_view .onAppear { self.isPresented = true } .onDisappear { self.isPresented = false } } } 
Sign up to request clarification or add additional context in comments.

Comments

0

I've solved this by changing the variable isPresented to @Binding, and modifying it then triggers an update on the subclass.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.