Swift UI - 我不明白为什么这个按钮不会打印我设置的动作

Swift UI - I can't figure out why this button won't print the action I set

我一直在努力弄清楚为什么我的按钮不执行我设置的操作。如有任何帮助,我们将不胜感激!

该代码通过发送一个动作作为输入来工作。我使用代码中链接的另一个堆栈溢出来获取它,但我不太明白为什么它不会执行该操作。我尝试在视图中设置另一个按钮,但它也不会执行该操作。请帮忙!

//
//  LargeButton.swift
//  Plan-It
//
//  Created by Aarya Chandak on 5/13/22.
// 

import SwiftUI

struct LargeButtonStyle: ButtonStyle {
    
    let backgroundColor: Color
    let foregroundColor: Color
    let isDisabled: Bool
    
    func makeBody(configuration: Self.Configuration) -> some View {
        let currentForegroundColor = isDisabled || configuration.isPressed ? foregroundColor.opacity(0.3) : foregroundColor
        return configuration.label
            .padding()
            .foregroundColor(currentForegroundColor)
            .background(isDisabled || configuration.isPressed ? backgroundColor.opacity(0.3) : backgroundColor)
            // This is the key part, we are using both an overlay as well as cornerRadius
            .cornerRadius(6)
            .overlay(
                RoundedRectangle(cornerRadius: 6)
                    .stroke(currentForegroundColor, lineWidth: 1)
        )
            .padding([.top, .bottom], 10)
            .font(Font.system(size: 19, weight: .semibold))
    }
}

struct LargeButton: View {
    
    private static let buttonHorizontalMargins: CGFloat = 20
    
    var backgroundColor: Color
    var foregroundColor: Color
    
    private let title: String
    private let action: () -> Void
    
    // It would be nice to make this into a binding.
    private let disabled: Bool
    
    init(title: String,
         disabled: Bool = false,
         backgroundColor: Color = Color.green,
         foregroundColor: Color = Color.white,
         action: @escaping () -> Void) {
        self.backgroundColor = backgroundColor
        self.foregroundColor = foregroundColor
        self.title = title
        self.action = action
        self.disabled = disabled
    }
    
    var body: some View {
        HStack {
            Spacer(minLength: LargeButton.buttonHorizontalMargins)
            Button(action:self.action) {
                Text(self.title)
                    .frame(maxWidth:.infinity)
            }
            .buttonStyle(LargeButtonStyle(backgroundColor: backgroundColor,
                                          foregroundColor: foregroundColor,
                                          isDisabled: disabled))
                .disabled(self.disabled)
            Spacer(minLength: LargeButton.buttonHorizontalMargins)
        }
        .frame(maxWidth:.infinity)
    }
}

struct LargeButton_Previews: PreviewProvider {
    static var previews: some View {
        LargeButton(title: "Invite a Friend",
                    backgroundColor: Color.white,
                    foregroundColor: Color.green) {
                                print("Hello World")
                            }
    }
}

您仅在预览代码中设置了按钮操作。

但它是印刷品,预览中的印刷品不会传送到 Xcode 的控制台。

在您的应用的实际 UI 和 运行 中定义操作,您应该会在控制台中看到 print 调用的结果。