发出警报

时间:2019-08-01 02:21:01

标签: xcode alert swiftui

我使用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 var shownAlert = false

谢谢。

1 个答案:

答案 0 :(得分:0)

您将使用类似下面的代码。如果您使用的是onTapGesture,则不需要按钮。

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")))
        })
    }
}
相关问题