Andoni Da Silva
Andoni Da Silva

Reputation: 1341

Creating a new VIPER module from Wireframe using SwiftUI

I am trying to implement VIPER using SwiftUI. Firstly, I was returning a UIKit VC(UIHostingController(rootView)) from the Wireframe until I realized that in navigation, when presenter tells Wireframe to present a SwiftUI View, I can not pass a UIViewController trough a NavigationLink.

Having this issue, I tried to make Wireframe protocol to return a SwiftUI View, but it seems to be no possible

Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements

final class HomeWireFrame: HomeWireFrameProtocol {
//Error here returning a View
class func createHomeModule() -> View {

    var view = HomeView()
    //set up VIPER modules...
    .
    .

    return view

How can a I define a function that will return a SwiftUI View?

Upvotes: 3

Views: 706

Answers (2)

Vasilis D.
Vasilis D.

Reputation: 1456

Your Presenter class should have a variable of the protocol that the view implements. So lets say that your Presenter who implements the ModuleInput and ModuleOutput Protocols has 2 variables on for the view of type ViewInput Protocol and one for the router of type RouterInput Protocol.

So you router Input classes and Router should be like this

 protocol RouterInput {
    func routeToAnotherView(from view: ViewInput)
}

class Router: RouterInput {
    func routeToAnotherView(from view: ViewInput){
        guard let viewInMyType = view as? MyType else {
            return
        }
        // Now you can go to your view
    }
}

And from presenter you will call the function with the parameter your View protocol outlet.

Upvotes: 2

Andoni Da Silva
Andoni Da Silva

Reputation: 1341

I have solved this issue returning a "HomeViewProtocol" instead a View... My HomeView conforms the HomeViewProtocol and of course, is a SwiftUI view.

Upvotes: 0

Related Questions