小编Luk*_*ers的帖子

SwiftUI - 隐藏 ScrollView 的指示器使其停止滚动

我试图隐藏 a 的指示器,ScrollView但是当我尝试这样做时,ScrollView它不再滚动。如果这很重要,我正在使用 macOS。

ScrollView(showsIndicators: false) {
    // Everything is in here
}
Run Code Online (Sandbox Code Playgroud)

macos swift swiftui

10
推荐指数
1
解决办法
2358
查看次数

仅设置一次状态变量后警报显示两次

我想让这个按钮运行某个命令,如果失败,我希望它显示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)

swift swiftui

5
推荐指数
1
解决办法
1872
查看次数

如何在SwiftUI的一个视图上显示两个警报?

我想将两个唯一的警报附加到同一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 …

swift swiftui

4
推荐指数
5
解决办法
251
查看次数

Swift - 类型 '' 不符合协议 'Hashable'

所以我有这个结构:

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。

swift

4
推荐指数
1
解决办法
3218
查看次数

标签 统计

swift ×4

swiftui ×3

macos ×1