8

While using storyboard we can programatically click a button or a field by using

@IBOutlet weak var negativeButton: UIButton!
negativeButton.sendActions(for: .touchUpInside)

How do I recreate the same using SwiftUI?

3 Answers 3

4

You can't treat the SwiftUI button like the UIKit button. Because In SwiftUI View (Button) doesn't have an object/outlet as you did in the storyboard so it doesn't have a function like that.

Instead, you can create the function and you can use that function to trigger the button action and you can call that function from anywhere wherever you required.

Please refer to the below example for more clear understanding.

struct ContentView: View {

    var body: some View {
        Button(action: {
            buttonAction()
        }, label: {
            Text("Tap Here")
        })
    }

    func buttonAction(){
        //do write here your button action logic
        print("button tapped")
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

This will definitely work but if the label is a NavigationLink then this wont. any idea with that? isActive is deprecated.
1

It is impossible to do the same in SwiftUI, SwiftUI does not know even Button exist "As Object! But as Render and it's duty exist", the why of using a Button is it's functionality in SwiftUI, that means firing up an Action or ()-> Void, so you do not need also you cannot programatically tap the Button, because you can run anytime and anywhere it's action.

For example: you can run actionOfButton() from the place you want programmatically tap the Button, it would work the same. if your Button changes it's appearance depending on each tap, you should make does happen in the actionOfButton(), So with that said, you could have action and render at the same time, and that's Wrap-Up for tapping a Button programatically in SwiftUI.

import SwiftUI

struct ContentView: View {
    var body: some View {

        Button("tap") {
            
            actionOfButton()
            
        }

    }
    
    func actionOfButton() {
        
        print("Hello, world!")
        
    }
    
}

1 Comment

Sure but isn't this problematic? How can I test that when I tap the button a certain method is called? If this is impossible to achieve then the only way to test this is by using UITest. Anyone has any idea if I am missing something?
0

The possible solution is to declare your action variables inside your structure view and whenever you need you can manually call the button action.

Here is the demo.

struct ContentViewButtonAction: View {
    
    var buttonAction: ()->Void {
        {
            print("Button tapped!!")
        }
    }
    
    var body: some View {
        Button(action: buttonAction, label: {
            Text("Tap Button")
        })
        
        Button(action: {
            print("Second button tapped")
            buttonAction()
        }, label: {
            Text("Tap Button to execute first button action")
        })
    }
}

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.