ARTICLE AD BOX
I am trying to understand how to make a button execute an action and navigate to a new UI, like so (the code below is inside a simple Struct):
Button(action: { print("I am new to SwiftUI!") buttonFunction() }) { NavigationLink { NewUI() } label: { Image(buttonImage) .resizable() .frame(maxWidth:50, maxHeight:50) }With the above, I get the navigation to the NewUI to execute, but the button's action does not. The print statement never prints.
In then tried to inverse it like so:
NavigationLink { NewUI() } label: { Button(action: { print("Oh no, this only prints!") buttonFunction() }) { Image(buttonImage) .resizable() .frame(maxWidth:50, maxHeight:50) }But this time it only prints the statement (buttonFunction executes), the navigation does not execute.
I was thinking maybe to solution involves migrating the navigation link call to the function, like so:
func buttonFunction() { // do some cool function stuff here // once done, somehow call for the navigation to execute }However, it doesn't appear to me I can place that code inside a function. When I tried it resulted in errors.
What is a solution for this?
I also read through some of the similar questions here, some referred to TapGesture(). I tried that too but no luck.
