SwiftUI How to push to next screen when tapping on Button avoid @state

I want push and post a text to next screen when tapping on Button. But NavigationLink need @state property to catch data. How to avoid this? I hope there is a way I only handle data in button action.

Code Block Swift
@State var selection: Int? = nil
  @State var text: String? = nil
   
  var body: some View {
    VStack(content: {
      HStack(alignment: .center, spacing: nil, content: {
        NavigationLink(destination: Drawing(text: text), tag: 1, selection: $selection) {
          Button("complete", action: {
            print(self.selection)
            print(self.text)
            self.text = "hello"
            self.selection = 1
          })
        }
      }).padding()


Answered by OOPer in 674373022

I think this is a bit difficult to understand compared with the commonly used implementation. For example, It's does not need set some property in UIViewController for post data to next UIViewController when tap a button.

Seems the best way for you is to go back to the UIKit world.

How to avoid this? 

Why do you want to avoid it?

And why are you using a Button as a label of the NavigationLink?

If you could explain what you really want to do, there may be some ways. But currently, you look just trying something strange.
I think this is a bit difficult to understand compared with the commonly used implementation. For example, It's does not need set some property in UIViewController for post data to next UIViewController when tap a button.

Code Block Swift
@IBAction func complete(_ sender: Any) {
let text = "some data"
   let drawingViewController = DrawingViewController(text: text)
navigationController?.pushViewController(drawingViewController, true)
}


Accepted Answer

I think this is a bit difficult to understand compared with the commonly used implementation. For example, It's does not need set some property in UIViewController for post data to next UIViewController when tap a button.

Seems the best way for you is to go back to the UIKit world.
Seems the best way for me is go to the Flutter world. HeHe.

Seems the best way for me is go to the Flutter world. HeHe.

Congratulations for finding the best for you. May your app be great.
SwiftUI How to push to next screen when tapping on Button avoid @state
 
 
Q