Protocol
ViewControllable
A protocol that provides access to the underlying platform view controller.
@MainActor protocol ViewControllable : AnyObject
Mentioned In
Overview
ViewControllable serves as the bridge between the napkin architecture and the platform’s view controller (UIKit or AppKit). It allows routers to manage view controller presentation and hierarchy without depending on concrete view controller types.
Overview
All view controllers used with ViewableRouter must conform to this protocol. The protocol provides a single property that returns the underlying view controller.
UIKit View Controllers
For standard UIKit view controllers, conformance is automatic via the default implementation:
final class MyViewController: UIViewController, MyViewControllable {
// Automatically conforms via default implementation
}
SwiftUI Views
For SwiftUI views, wrap them in a UIHostingController (iOS) or NSHostingController (macOS):
import SwiftUI
protocol MyViewControllable: ViewControllable {}
final class MyHostingController: UIHostingController<MySwiftUIView>,
MyViewControllable {
init(viewModel: MyViewModel, listener: MyViewListener) {
let view = MySwiftUIView(viewModel: viewModel, listener: listener)
super.init(rootView: view)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Defining Feature-Specific Protocols
Define a feature-specific protocol that extends ViewControllable:
protocol MyFeatureViewControllable: ViewControllable {
func displayData(_ data: MyViewModel)
func displayError(_ message: String)
}
See Also
ViewableRouter
See Also
ViewableRouting
Topics
Instance Properties