Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot assign value of type 'MenuView' to type 'some View'

Tags:

swift

swiftui

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.

For that I have:

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

and then the modifier:

.popover(isPresented: self.$showPopover) {
    self.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:

Cannot assign value of type 'MenuView' to type 'some View'

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

like image 942
Rivera Avatar asked Sep 07 '25 05:09

Rivera


1 Answers

The solution was to use AnyView:

@State private var popover: AnyView

Then it can be assigned as:

self.popover = AnyView(CreateChannelView(showing: self.$showPopover))
like image 156
Rivera Avatar answered Sep 10 '25 03:09

Rivera