【问题标题】:Presenting an alert提出警报
【发布时间】:2019-08-01 02:21:01
【问题描述】:

我使用 Xcode 11 Beat 5 运行 SwiftUI,我希望添加一个警报以在点击按钮时向用户显示。

但我遇到了问题。

HStack{
    Text("Inverted V")
      .font(.largeTitle)
      .fontWeight(.bold)
      .foregroundColor(Color.black)
    Spacer()
    Button(action: {

    }, label: {
      Image(systemName: "questionmark.circle.fill")
      .resizable()
      .frame(width: 30, height: 30)
      .foregroundColor(.red)

    }).onTapGesture {
      print("Hello Print")

  }}

有人可以帮助提供所需的代码吗?我是否将它放在 onTapGesture 括号内?

我有一个 @State 变量显示Alert = false

提前谢谢你。

【问题讨论】:

  • 嗨,感谢您的帮助和建议,但我不确定这是否对我有帮助...我正在使用 TapGesture,因为它在列表中...此时有人告诉我按钮不起作用,但会选择整行。所以 TapGesture 工作正常。我只是不确定如何在 TapGesture 被点击时显示警报。谢谢..希望这是有道理的..我是新手。

标签: xcode alert swiftui


【解决方案1】:

您将使用类似于以下代码的内容。如果您使用的是 onTapGesture,则不需要 Button。

import SwiftUI

struct ContentView: View {
    @State var showingAlert: Bool = false
    var body: some View {
        HStack{
            Text("Inverted V")
                .font(.largeTitle)
                .fontWeight(.bold)
                .foregroundColor(Color.black)
            Spacer()

            Image(systemName: "questionmark.circle.fill")
                .resizable()
                .frame(width: 30, height: 30)
                .foregroundColor(.red)
                .onTapGesture {
                    print("Hello Print")
                    self.showingAlert = true
            }
        }.alert(isPresented: $showingAlert, content: {
            Alert(title: Text("Error"), message: Text("Error Reason"), dismissButton: .default(Text("OK")))
        })
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多