简体   繁体   English

SwiftUI 绑定 boolean if 语句(无法转换类型“绑定”的值<bool> ' 到预期的条件类型 'Bool')</bool>

[英]SwiftUI binding boolean if statement (Cannot convert value of type 'Binding<Bool>' to expected condition type 'Bool')

I'm trying to conditionally show a View (not using.sheet) and using a binding boolean returns the error above in the title.我试图有条件地显示一个视图(不使用.sheet)并使用绑定 boolean 在标题中返回上述错误。 If I use self.menuactivated , no view will be presented upon the @State variable toggle.如果我使用self.menuactivated ,@State 变量切换时不会显示任何视图。 Is there a way around this?有没有解决的办法?

struct ContentView: View {
    @State private var menuActivated  = false

    var body: some View {
        NavigationView {
            ... // code
        }
        if $menuActivated {
            menuView()
        }
    }
}

I assume this was the intention (menu will be shown over NavigationView on toggle)我认为这是意图(菜单将在切换时显示在 NavigationView 上)

struct ContentView: View {
    @State private var menuActivated  = false

    var body: some View {
        ZStack {
           NavigationView {
            ... // code
           }
           if menuActivated {
              menuView()
           }
        }
    }
}

Found a way around it.找到了解决方法。 Embedded the NavigationView in a VStack and added the menuView to the end of it.NavigationView嵌入到VStack中,并将menuView添加到它的末尾。

struct ContentView: View {        
@State private var menuActivated  = false

        var body: some View {
            ZStack {

                NavigationView {
                ... // view code        
                }
                if menuActivated {
                    menuView()
                }

            }

        }
    }

in case if you are looking for condition based view如果您正在寻找基于条件的视图

func getView() -> AnyView {
    return AnyView(
        //any business logic and view  
    )
}

and use it like并像使用它一样

self.getView()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法转换“已发布”类型的值<bool> .Publisher'到预期的参数类型'绑定<bool> '</bool></bool> - Cannot convert value of type 'Published<Bool>.Publisher' to expected argument type 'Binding<Bool>' SwiftUI 错误修复“无法转换类型‘绑定’的值<int> &#39;到预期类型&#39;绑定&lt;_&gt;?&#39;” - SwiftUI Bug Fix "Cannot convert value of type 'Binding<int>'to expected type 'Binding<_>?'" SwiftUi- 无法将 Binding<[ExercisePlan]>.Element 类型的值转换为预期的参数类型 Binding<[ExercisePlan]> - SwiftUi- Cannot convert value of type Binding<[ExercisePlan]>.Element to expected argument type Binding<[ExercisePlan]> 无法将Int类型的值转换为预期的参数类型Bool - Cannot convert value of type Int to expected argument type Bool 无法将类型&#39;(_)-&gt;()&#39;的值转换为预期的参数类型&#39;((Bool,Error?)-&gt; Void)? - Cannot convert value of type '(_) -> ()' to expected argument type '((Bool, Error?) -> Void)?' 无法将“KotlinBoolean”类型的值转换为预期的参数类型“Bool” - Cannot convert value of type 'KotlinBoolean' to expected argument type 'Bool' 无法将类型&#39;(_) - &gt; Bool&#39;的值转换为预期的参数类型&#39;NSPredicate&#39; - Cannot convert value of type '(_) -> Bool' to expected argument type 'NSPredicate' 无法将“字符串”类型的值转换为预期的参数类型“布尔” - Cannot convert value of type 'String' to expected argument type 'Bool' Swift:无法将类型&#39;() - &gt; Bool&#39;的值转换为预期的参数类型&#39;PFObject&#39; - Swift: Cannot convert value of type '() -> Bool' to expected argument type 'PFObject' 无法将类型()的值转换为预期的参数类型bool(Swift)? - Cannot convert value of type () to expected argument type bool (Swift)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM