[SwiftUI] Navigation links with UIViewControllers as destination

  1. I currently create a corresponding view conforming to UIViewControllerRepresentable for each of the UIViewControllers and present them like so :face_vomiting:
  2. Thinking of setting up a view protocol and make the views I wish to present conform to that and just set the properties there and
  3. Iterate through an enum of the aforementioned views

What’s a better way to structure this?

struct MyRoutesSectionViewObject: Hashable {
 var title: String
 var iconString: String
 var count: Int
 var badgeIconString: String?
}

enum MyRoutesType: CaseIterable, Hashable {
 case savedRoutes(MyRoutesSectionViewObject)
 case plannedRoutes(MyRoutesSectionViewObject)
 case recordedRoutes(MyRoutesSectionViewObject)
 case offlineRoutes(MyRoutesSectionViewObject)
 static var allCases: [MyRoutesType] {
  return [.savedRoutes(MyRoutesSectionViewObject(title: "Saved",
                          iconString: "savedIcon",
                          count: 10)),
      .plannedRoutes(MyRoutesSectionViewObject(title: "Planned",
                           iconString: "plannedIcon",
                           count: 10)),
      .recordedRoutes(MyRoutesSectionViewObject(title: "Recorded",
                           iconString: "recordedIcon",
                           count: 10)),
      .offlineRoutes( MyRoutesSectionViewObject(title: "Offline",
                           iconString: "offlineIcon",
                           count: 10,
                           badgeIconString: "premiumBadge"))]
 }
}

struct MyRoutesSectionView: View {
 var body: some View {
  ForEach(MyRoutesType.allCases, id: \.self) { routeType in
   switch routeType {
   case .savedRoutes(let routeObject):
    NavigationLink(destination: SavedRoutesView()) {
     MyRoutesNavigationLinkView(myRoutesObject: routeObject)
    }
   case .plannedRoutes(let routeObject):
    NavigationLink(destination: PlannedRoutesView()) {
     MyRoutesNavigationLinkView(myRoutesObject: routeObject)
    }
   case .recordedRoutes(let routeObject):
    NavigationLink(destination: RecordedRoutesView()) {
     MyRoutesNavigationLinkView(myRoutesObject: routeObject)
    }
   case .offlineRoutes(let routeObject):
    NavigationLink(destination: SavedRoutesView()) {
     MyRoutesNavigationLinkView(myRoutesObject: routeObject)
    }
   }
  }
 }
}

Hi @lganti,
Is this question before your other question Conditional views + ViewBuilder ? does that take over this or is this still something you are seeking an answer for?

1 Like

Yep, before; this post can be considered void, thanks for asking!

Hi @lganti, no worries, :+1:

This topic was automatically closed after 166 days. New replies are no longer allowed.