本文介绍了无法将“MenuView"类型的值分配给“some View"类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在 SwiftUI 中只能有一个 .popover 修饰符.我必须展示两个可能的简单弹出框,其中一个是 MenuView,另一个是 CreateChannelView.

I noticed that you can only have a single .popover modifier in SwiftUI. I have to present two possible simple popovers, one of them a MenuView, the other a CreateChannelView.

为此我有:

@State private var popover: some View
@State private var showPopover = false

然后是修饰符:

.popover(isPresented: self.$showPopover) {
    self.popover
}

问题是我不知道如何在出现错误时将 MenuViewCreateChannelView 的实例分配给 popover:

The problem is that I don't see how can I assign instances of MenuView or CreateChannelView to popover as I get the error:

无法将MenuView"类型的值分配给some View"类型

这与 this question 稍有不同,后者在 init 中传递通用视图方法.

This is a little bit different than this question which passes generic views in the init method.

推荐答案

解决方案是使用 AnyView:

@State private var popover: AnyView

那么它可以被赋值为:

self.popover = AnyView(CreateChannelView(showing: self.$showPopover))

这篇关于无法将“MenuView"类型的值分配给“some View"类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 07:02
查看更多