Switching FocusState in func subview

I finally managed to have the last part of a 2.0 update to my app working fine. It involves a save view where the two players can attach their names to the stats of the last game just played.

As part of this view, I needed an auto suggest list under each of the textFields to appear as the players enter their names. This is so they don't have duplicate entries for variations of their names (for example, they might use "Daniel" or "Danny", but not sure which they used in the past).

It all worked great, EXCEPT that when I put the code for both autosuggest lists, the view became too unwieldy for Xcode to compile.

So: break into subviews. But, there is so much interconnected logic among all the pieces of the save screen that (given my very limited conceptual understanding of SwiftUI) I got horribly bogged down with @Bindings and passing parameters.

I did manage to break the autosuggest lists into their own subview (as a func). It now compiles (yay!). One little caveat, which is why I'm asking you for help. Here is (finally™) the ...

question:

Given the autosuggest list, I'd like the behavior to be that when the player taps on their name, the focus goes to the other player's textField. The FocusState is a Hashable enum called "FocusableField" with two cases (player1 and player2).

I'm having trouble changing the FocusState in the subview func. I tried to pass the FocusState var parameter as Binding<FocusableField> and as inout FocusableField, adding the appropriate $/&, but I get (respectively):

Cannot convert value of type 'FocusState<FocusableField?>.Binding' to expected argument type 'Binding'

and

Escaping closure captures 'inout' parameter 'focus'

Thanks for any help/suggestion.

Answered by aleand67 in 718990022

Fixed it. Put the func inside the main view (save view) struct, so I didn't need to pass the FocusState as a parameter. Put a switch statement (isn't there a better way to cycle through enums?) to get desired behavior. Code is pukeful looking, but: it works!

Accepted Answer

Fixed it. Put the func inside the main view (save view) struct, so I didn't need to pass the FocusState as a parameter. Put a switch statement (isn't there a better way to cycle through enums?) to get desired behavior. Code is pukeful looking, but: it works!

Switching FocusState in func subview
 
 
Q