【问题标题】:SwiftUI ActionSheet does not dismiss when timer is running计时器运行时,SwiftUI ActionSheet 不会关闭
【发布时间】:2020-11-22 12:23:31
【问题描述】:

我有以下简单的 SwiftUI 设置。正在运行和更新文本的计时器。如果计时器没有运行(停止或暂停),我可以轻松地显示一个 ActionSheet(通过点击 Actions)并通过选择“Cancel”或“Action 1”选项将其关闭。但是,如果计时器正在运行,我很难通过选择“取消”或“操作 1”选项之一来关闭 ActionSheet。你知道发生了什么事吗?

我使用的是 Xcode 11.5。

import SwiftUI

struct ContentView: View {
    
    @ObservedObject var stopWatch = StopWatch()
    @State private var showActionSheet: Bool = false
    
    var body: some View {
        VStack {
            Text("\(stopWatch.secondsElapsed)")
            HStack {
                if stopWatch.mode == .stopped {
                    Button(action: { self.stopWatch.start() }) {
                        Text("Start")
                    }
                } else if stopWatch.mode == .paused {
                    Button(action: { self.stopWatch.start() }) {
                        Text("Resume")
                    }
                } else if stopWatch.mode == .running {
                    Button(action: { self.stopWatch.pause() }) {
                        Text("Pause")
                    }
                }
                Button(action: { self.stopWatch.stop() }) {
                    Text("Reset")
                }
            }
            Button(action: { self.showActionSheet = true }) {
                Text("Actions")
            }
            .actionSheet(isPresented: $showActionSheet) {
                ActionSheet(title: Text("Actions"), message: nil, buttons: [.default(Text("Action 1")), .cancel()])
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
import SwiftUI

class StopWatch: ObservableObject {
    
    @Published var secondsElapsed: TimeInterval = 0.0
    @Published var mode: stopWatchMode = .stopped
    
    var timer = Timer()
    func start() {
        mode = .running
        timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { timer in
            self.secondsElapsed += 0.1
        }
    }
    
    func stop() {
        timer.invalidate()
        secondsElapsed = 0
        mode = .stopped
    }
    
    func pause() {
        timer.invalidate()
        mode = .paused
    }
    
    enum stopWatchMode {
        case running
        case stopped
        case paused
    }
}

【问题讨论】:

    标签: ios swift swiftui actionsheet swiftui-actionsheet


    【解决方案1】:

    适用于 Xcode 12 / iOS 14,但尝试将带有工作表的按钮分离到另一个子视图中,以避免在计时器刷新时重新创建它。

    使用 Xcode 12 / iOS 14 测试

    struct ContentView: View {
    
        @ObservedObject var stopWatch = StopWatch()
        // @StateObject var stopWatch = StopWatch()       // << used for SwiftUI 2.0
        @State private var showActionSheet: Bool = false
    
        var body: some View {
            VStack {
                Text("\(stopWatch.secondsElapsed)")
                HStack {
                    if stopWatch.mode == .stopped {
                        Button(action: { self.stopWatch.start() }) {
                            Text("Start")
                        }
                    } else if stopWatch.mode == .paused {
                        Button(action: { self.stopWatch.start() }) {
                            Text("Resume")
                        }
                    } else if stopWatch.mode == .running {
                        Button(action: { self.stopWatch.pause() }) {
                            Text("Pause")
                        }
                    }
                    Button(action: { self.stopWatch.stop() }) {
                        Text("Reset")
                    }
                }
                ActionsSubView(showActionSheet: $showActionSheet)
            }
        }
    }
    
    struct ActionsSubView: View {
        @Binding var showActionSheet: Bool
        var body: some View {
            Button(action: { self.showActionSheet = true }) {
                Text("Actions")
            }
            .actionSheet(isPresented: $showActionSheet) {
                ActionSheet(title: Text("Actions"), message: nil, buttons: [.default(Text("Action 1")), .cancel()])
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-09
      • 1970-01-01
      • 2021-03-27
      • 2018-05-24
      • 2021-11-27
      • 1970-01-01
      • 2022-11-27
      • 2018-10-12
      相关资源
      最近更新 更多