xcode - 提出警报
问题描述
我使用 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 显示Alert = false
先感谢您。
解决方案
您将使用类似于以下代码的内容。如果您使用的是 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")))
})
}
}
推荐阅读
- python - 从具有细分的多索引数据框中绘制多个 x 轴线图以匹配比例并可视化趋势比较
- css - 使用 Inkscape 将外部 CSS 类样式嵌入到 SVG 元素中
- c# - webBrowser1.AllowWebBrowserDrop 引发了“System.Runtime.InteropServices.InvalidComObjectException”类型的异常
- angular - NgZone() 如何在 Microsoft Teams 选项卡中工作
- esp32 - 电涌后esp32重新连接
- javascript - Postman - 如何存储多个请求的随机值?
- java - WebDriverException: Timed out waiting for driver server to start error with Selenium Edge Chromium through Java
- javascript - react 17.0.01 中对象数组的动态表值
- javascript - ckeditor 停止传播
- if-statement - SSRS 报告 - IIF statmen