切换 swiftUI toggle() 时如何触发操作? [英] How can I trigger an action when a swiftUI toggle() is toggled?

查看:63
本文介绍了切换 swiftUI toggle() 时如何触发操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 SwiftUI 视图中,当 Toggle() 更改其状态时,我必须触发一个操作.切换本身只需要一个绑定.因此,我尝试在 @State 变量的 didSet 中触发操作.但是 didSet 永远不会被调用.

In my SwiftUI view I have to trigger an action when a Toggle() changes its state. The toggle itself only takes a Binding. I therefore tried to trigger the action in the didSet of the @State variable. But the didSet never gets called.

是否有任何(其他)方式来触发操作?或者有什么方法可以观察@State变量的值变化?

Is there any (other) way to trigger an action? Or any way to observe the value change of a @State variable?

我的代码如下所示:

struct PWSDetailView : View {

    @ObjectBinding var station: PWS
    @State var isDisplayed: Bool = false {
        didSet {
            if isDisplayed != station.isDisplayed {
                PWSStore.shared.toggleIsDisplayed(station)
            }
        }
    }

    var body: some View {
            VStack {
                ZStack(alignment: .leading) {
                    Rectangle()
                        .frame(width: UIScreen.main.bounds.width, height: 50)
                        .foregroundColor(Color.lokalZeroBlue)
                    Text(station.displayName)
                        .font(.title)
                        .foregroundColor(Color.white)
                        .padding(.leading)
                }

                MapView(latitude: station.latitude, longitude: station.longitude, span: 0.05)
                    .frame(height: UIScreen.main.bounds.height / 3)
                    .padding(.top, -8)

                Form {
                    Toggle(isOn: $isDisplayed)
                    { Text("Wetterstation anzeigen") }
                }

                Spacer()
            }.colorScheme(.dark)
    }
}

期望的行为是当 Toggle() 改变其状态时触发操作PWSStore.shared.toggleIsDisplayed(station)".

The desired behaviour would be that the action "PWSStore.shared.toggleIsDisplayed(station)" is triggered when the Toggle() changes its state.

推荐答案

首先,你真的知道 station.isDisplayed 额外的 KVO 通知有问题吗?您是否遇到性能问题?如果没有,那就别担心.

First, do you actually know that the extra KVO notifications for station.isDisplayed are a problem? Are you experiencing performance problems? If not, then don't worry about it.

如果您遇到性能问题并且您已经确定它们是由于过多的 station.isDisplayed KVO 通知造成的,那么接下来要尝试消除不需要的 KVO 通知.您可以通过切换到手动 KVO 通知来实现.

If you are experiencing performance problems and you've established that they're due to excessive station.isDisplayed KVO notifications, then the next thing to try is eliminating unneeded KVO notifications. You do that by switching to manual KVO notifications.

将此方法添加到station的类定义中:

Add this method to station's class definition:

@objc class var automaticallyNotifiesObserversOfIsDisplayed: Bool { return false }

并使用 Swift 的 willSetdidSet 观察者手动通知 KVO 观察者,但前提是值发生变化时:

And use Swift's willSet and didSet observers to manually notify KVO observers, but only if the value is changing:

@objc dynamic var isDisplayed = false {
    willSet {
        if isDisplayed != newValue { willChangeValue(for: \.isDisplayed) }
    }
    didSet {
        if isDisplayed != oldValue { didChangeValue(for: \.isDisplayed) }
    }
}

这篇关于切换 swiftUI toggle() 时如何触发操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆