POP で私の最初のクラックを持っています。この場合、いくつかの UIViewControllers をデコレートして、「ページ表示」分析イベントを自動的に発生させるようにします。
そこで、プロトコルと、そのプロトコルの拡張機能を作成しました。
protocol ReportPageViewedEvent {
func reportPageViewed()
var pageName : String? { get set }
}
extension ReportPageViewedEvent where Self: UIViewController
{
func reportPageViewed()
{
guard let pageName = self.pageName else
{
fatalError("UIViewController implements ReportPageViewEvent protocol but did not set pageName property")
}
let eventBusiness = EventBusiness.sharedInstance
eventBusiness.logUserViewedPage(pageName)
}
}
UIViewController を次のように装飾すると、これは私が望むように機能しReportPageViewedEventます。
class HomeView: UIViewController, ReportPageViewedEvent {
正確に必要な「pageName」を設定しないと、コンパイラ エラーが発生します。
reportPageViewed()私が行き詰まっているのは、実際のメソッドをどこでどのように呼び出すかです。viewDidLoadつまり、それを使用するすべてのコントローラーのすべての「viewDidLoad」を変更するか、サブクラス化して、最初にPOPを使用するという点に反するスーパークラスのメソッドを呼び出す必要があります。
これを達成する良い方法はありますか。このような例は、どのチュートリアル/ブログでも見つかりません。