简体   繁体   English

如何在 SwiftUI 中以编程方式单击按钮

[英]How to click a button programmatically in SwiftUI

While using storyboard we can programatically click a button or a field by using在使用 storyboard 时,我们可以通过使用以编程方式单击按钮或字段

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

How do I recreate the same using SwiftUI?如何使用 SwiftUI 重新创建相同的内容?

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.在 SwiftUI 中做同样的事情是不可能的,SwiftUI 甚至不知道按钮存在“作为对象!但是作为渲染并且它的职责存在” ,为什么使用按钮是它在Z5D807DF8E2D0F60FCC3ZB5037功能)-> Void ,所以你不需要不能以编程方式点击按钮,因为你可以随时随地运行它的动作。

For example: you can run actionOfButton() from the place you want programmatically tap the Button, it would work the same.例如:您可以从您想要以编程方式点击按钮的位置运行actionOfButton() ,它的工作方式相同。 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.如果您的 Button 根据每次点击改变它的外观,您应该在actionOfButton()中发生,因此,您可以同时进行操作和渲染,这就是在 SwiftUI 中以编程方式点击按钮的总结.

import SwiftUI

struct ContentView: View {
    var body: some View {

        Button("tap") {
            
            actionOfButton()
            
        }

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

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")
        })
    }
}

在此处输入图像描述

You can't treat the SwiftUI button like the UIKit button.您不能将 SwiftUI 按钮视为 UIKit 按钮。 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.因为在 SwiftUI 中,视图(按钮)没有像在 storyboard 中那样的对象/插座,所以它没有这样的 function。

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.相反,您可以创建 function 并且可以使用该 function 来触发按钮操作,并且您可以从任何需要的地方调用该 function。

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")
    }

}

For testing SwiftUI button programmatically, you can use the hidden _ViewTest protocol that contains a sendTouchSequence function.要以编程方式测试 SwiftUI 按钮,您可以使用包含 sendTouchSequence function 的隐藏 _ViewTest 协议。

This article explains it at the end: https://betterprogramming.pub/behavioural-testing-of-swiftui-views-with-host-app-and-viewtest-f1343a281c5f本文最后解释: https://betterprogramming.pub/behavioural-testing-of-swiftui-views-with-host-app-and-viewtest-f1343a281c5f

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM