Ternary operator inside NavigationLink SwiftUI

后端 未结 1 2010
借酒劲吻你
借酒劲吻你 2021-01-23 20:34

I have a problem when trying to user ternary operator on a button in NavigationLink. I have an array of campaigns, which is displayed in a carouselView. Bellow carousel there is

相关标签:
1条回答
  • 2021-01-23 21:01

    This is because the types of CampaignDetailsView and Text don't match. You need to use a @ViewBuilder (or just a Group, VStack etc).

    Here is a solution with the destination view as a @ViewBuilder computed property:

    @ViewBuilder
    var destinationView: some View {
        if cardCampaigns.count > 0 {
            CampaignDetailsView(viewModel: CampaignDetailsViewModel(campaign: cardCampaigns[self.count].campaign))
        } else {
            Text("No Campaign found")
        }
    }
    

    Which you can use like this:

    NavigationLink(destination: destinationView) { ... }
    
    0 讨论(0)
提交回复
热议问题