在 SwiftUI 中点击时从函数返回的按钮不起作用

问题描述

所以我的 ContentView 中有一个 ForEach 循环,它多次调用 makeButton() 函数。此函数返回一个按钮,但由于某种原因,当您单击该按钮时,该按钮不会执行任何操作(也称为“动作”不会发生)。我想也许这与返回类型有关,但我真的不确定这里发生了什么:

ForEach(array,id: \.self) { (client: Client) in
    self.makeButton(client: client)
}

然后函数本身看起来像:

func makeButton(client: SocketClient) -> some View {  
    //calculations for horizontal and vertical offset here
    return Button(action: {
        print("Clicked a button and it should print out \(client.colorVariable)")
    },label: {
        ZStack {
            Circle()
                .fill(client.colorVariable)
        }.frame(width: 30,height: 30)
        .offset(x: horizontalOffset,y: verticalOffset)
    })
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)