在模拟器/预览版上运行的 SwiftUI:切换不起作用,控制台日志:“无效模式 'kCFRunLoopCommonModes'" [英] SwiftUI running on Simulator/Preview: Toggle not working, console logs: "invalid mode 'kCFRunLoopCommonModes'"

查看:33
本文介绍了在模拟器/预览版上运行的 SwiftUI:切换不起作用,控制台日志:“无效模式 'kCFRunLoopCommonModes'"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我最初的问题是问如何禁用导航链接,并且仅当影响两个 @State var isXYZToggleOn Bool 属性的两个 Toggle 都是 true<时才启用导航链接/强>.这一直有效,我第一次尝试使用 .disabled(!(hasAgreedToTermsAndConditions && hasAgreedToPrivacyPolicy)) 是正确的(@superpuccio 也建议,但使用两个否定和一个布尔值或(||)).

So my original question asked how to make a navigation link disabled and only enabled if two Toggle affecting two @State var isXYZToggleOn Bool properties are both true. This has always been working, my first attempt using .disabled(!(hasAgreedToTermsAndConditions && hasAgreedToPrivacyPolicy)) was the correct one (also suggested by @superpuccio, but using two negatations and one boolean or (||)).

原来我没有启用我的 NavigationLink,因为切换不起作用,而不是因为布尔值和 disabled 视图修饰符的错误使用.

Turns I did not get my NavigationLink to be enabled because the toggling did not work, not because of incorrect usage of the booleans and disabled view modifier.

在设备上运行,而不是模拟器使一切正常!但是我一按任何Toggle(但只有一次),我仍然会看到警告消息:

Running on device, instead of simulator made everything work! But I still see the warning message as soon as I press any Toggle (but only once):

invalid mode 'kCFRunLoopCommonModes' provided to CFRunLoopRunSpecific - break on _CFRunLoopError_RunCalledWithInvalidMode to debug. This message will only appear once per execution.

我也在模拟器上运行时收到此错误消息,但是 NavigationLink 从未启用.

I get this error message running on simulator too, but then the NavigationLink does not get enabled ever.

我在 2016 年 Macbook Pro 上的 Catalina 5 beta 上运行 Xcode 5 beta.删除派生数据,重新启动Xcode,重新启动我的电脑,重置模拟器,更改模拟器,没有任何帮助.当我按下第一个 Toggle 时,我仍然看到 invalid mode 'kCFRunLoopCommonModes',并且 NavigationLink 永远不会启用.

I'm running Xcode 5 beta on Catalina 5 beta on a 2016 Macbook Pro. Deleting derived data, restarting Xcode, restarting my computer, resetting the simulator, changing simulator, nothing helps. I still see invalid mode 'kCFRunLoopCommonModes' when I press the first Toggle, and the NavigationLink never gets enabled.

所以新问题是:

使用 XCode 11 beta 5 和 SwiftUI.在 WelcomeScene 中,我有两个 Toggle 视图,一个切换用于接受条款和;隐私政策的条件和一个开关.这些切换分别更新两个单独的 @State 属性.

Using XCode 11 beta 5 and SwiftUI. In a WelcomeScene I have two Toggle views, one toggle for accepting Terms & Conditions and one toggle for Privacy Policy. These toggles updates two separate @State properties respectively.

在场景的底部,我有一个 NagivationLink(按钮),它将转到下一个场景,我希望默认情况下禁用 并且仅在 both hasAgreedToTermsAndConditionshasAgreedToPrivacyPolicy 状态为 true启用>.

In the bottom of the scene I have a NagivationLink (button) which will take to the next scene, which I would like to be disabled by default and only be enabled when both hasAgreedToTermsAndConditions and hasAgreedToPrivacyPolicy states are true.

当启动一个 NavigationLink 时,有一个 isActive 参数,它需要一个 Binding,这听起来是正确的.但是,@Binding 属性不能被标记为 lazy,因此我不能使它成为依赖于 hasAgreedToTermsAndConditionshasAgreedToPrivacyPolicy.

When initiating a NavigationLink there is a isActive argument, which takes a Binding<Bool>, which sounds like the correct thing. However, @Binding properties cannot be marked lazy, thus I cannot make it a computed property being dependent on hasAgreedToTermsAndConditions and hasAgreedToPrivacyPolicy.

还有一个 disabled 视图修饰符,它接受一个 Bool,这也是不正确的,因为它没有得到更新......

There is also a disabled view modifier which takes a Bool, which is also incorrect since it does not get updated...

struct WelcomeScene: View {

    @State var hasAgreedToTermsAndConditions: Bool = false
    @State var hasAgreedToPrivacyPolicy: Bool = false

    var body: some View {
        VStack {
            Image(named: "MyImage")

            Spacer()

            Text("Welcome friend!".uppercased())
                .font(.system(size: 55))

            Toggle(isOn: $hasAgreedToTermsAndConditions) {
                Text("I agree to the Terms and Conditions")
            }.toggleStyle(DefaultToggleStyle())

            Toggle(isOn: $hasAgreedToPrivacyPolicy) {
                Text("I agree to the Privacy Policy")
            }.toggleStyle(DefaultToggleStyle())

            // This does not compile 'Binding<Bool> is not convertible to Bool', but I cannot figure out how to create a compupted property binding using those 2 states...
            NavigationLink("Proceed", destination: SignInScene(), isActive: ($hasAgreedToTermsAndConditions && $hasAgreedToPrivacyPolicy))
        }.padding(30)
    }
}

当两个开关都 on 时,如何默认设置 NavigationLink disabled 和仅 enabled?

How can I make a NavigationLink disabled by default and only enabled when both toggles are on?

推荐答案

这里的主要问题是您误解了您正在使用的 NavigationLink init 的含义.

The main issue here is that you are misunderstanding the meaning of the NavigationLink init you are using.

/// Creates an instance that presents `destination` when active, with a
/// `Text` label generated from a title string.
public init(_ titleKey: LocalizedStringKey, destination: Destination, isActive: Binding<Bool>)

isActive 并不意味着只有当 bool 为真时链接才可点击.这意味着您正在创建一个 NavigationLink 来触发导航 both 点击 并且当 isActive 绑定变为 true 时.

That isActive doesn't mean that the link is clickable only when that bool is true. That means that you are creating a NavigationLink that triggers the navigation both on click and when the isActive binding becomes true.

您可以通过以下方式使用 disabled 修饰符实现您想要的效果:

You can achieve what you want with the disabled modifier this way:

struct WelcomeScene: View {
    @State var hasAgreedToTermsAndConditions: Bool = false
    @State var hasAgreedToPrivacyPolicy: Bool = false

    var body: some View {
        NavigationView {
            VStack {

                Spacer()

                Toggle(isOn: $hasAgreedToTermsAndConditions) {
                    Text("I agree to the Terms and Conditions")
                }.toggleStyle(DefaultToggleStyle())

                Toggle(isOn: $hasAgreedToPrivacyPolicy) {
                    Text("I agree to the Privacy Policy")
                }.toggleStyle(DefaultToggleStyle())

                NavigationLink("Proceed", destination: SignInScene())
                    .disabled(!hasAgreedToTermsAndConditions || !hasAgreedToPrivacyPolicy)
            }.padding(30)
        }
    }
}

这篇关于在模拟器/预览版上运行的 SwiftUI:切换不起作用,控制台日志:“无效模式 'kCFRunLoopCommonModes'"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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