我试图隐藏 a 的指示器,ScrollView
但是当我尝试这样做时,ScrollView
它不再滚动。如果这很重要,我正在使用 macOS。
ScrollView(showsIndicators: false) {
// Everything is in here
}
Run Code Online (Sandbox Code Playgroud) 我想让这个按钮运行某个命令,如果失败,我希望它显示Alert
失败的信息。它做得很好,除了当警报显示时,它显示两次,但我只设置一次。
以下是我用来显示警报的两个状态变量:
@State private var alert = false
@State private var alertView = Alert(
title: Text("Well Hello There"),
message: Text("You probably shouldn't be seeing this alert but if you are, hello there! (This is a bug)")
)
Run Code Online (Sandbox Code Playgroud)
这是我的按钮:
Button(action: {
DispatchQueue.global(qos: .background).async {
if let command = action.command {
let error = Connection.shared.run(command: command)
if error != nil {
self.alertView = Alert(
title: Text("Failed to Run Action"),
message: Text("An error occurred while attempting to \(action.label).")
)
print("Displaying alert") …
Run Code Online (Sandbox Code Playgroud) 我想将两个唯一的警报附加到同一Button
视图。当我使用下面的代码时,只有底部的警报起作用。
我正在macOS Catalina上使用Xcode 11的正式版本。
@State private var showFirstAlert = false
@State private var showSecondAlert = false
Button(action: {
if Bool.random() {
showFirstAlert = true
} else {
showSecondAlert = true
}
}) {
Text("Show random alert")
}
.alert(isPresented: $showFirstAlert) {
// This alert never shows
Alert(title: Text("First Alert"), message: Text("This is the first alert"))
}
.alert(isPresented: $showSecondAlert) {
// This alert does show
Alert(title: Text("Second Alert"), message: Text("This is the second alert"))
}
Run Code Online (Sandbox Code Playgroud)
我希望在设置showFirstAlert
为true 时显示第一个警报,而在我设置为true时希望显示第二个警报showSecondAlert …
所以我有这个结构:
struct ListAction: Hashable {
let label: String
let action: (() -> Void)? = nil
let command: Command? = nil
}
Run Code Online (Sandbox Code Playgroud)
但是我在说Type 'ListAction' does not conform to protocol 'Hashable'
.
如果我删除定义action
常量的行,我可以摆脱错误,但我不想永久删除该行。
我正在使用 Swift 5.1。