Skip to content

Instantly share code, notes, and snippets.

@greggjaskiewicz
Created February 21, 2019 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greggjaskiewicz/70f4a37cf6abc51423c9ecbb0033d6c7 to your computer and use it in GitHub Desktop.
Save greggjaskiewicz/70f4a37cf6abc51423c9ecbb0033d6c7 to your computer and use it in GitHub Desktop.
UINavigationController doesn't send any feedback to view controllers, when they are about to appear on screen - if they are already in the stack. This solves this little problem.
//
// NavigationControllerFeedback.swift
// AmpetronicApp
//
// Created by Gregg Jaskiewicz on 21/02/2019.
// Copyright © 2019 Gregg Jaskiewicz. All rights reserved.
//
import Foundation
@objc protocol NavigationControllerFeedback {
@objc optional func willShow()
@objc optional func didShow()
}
final class NavigationControllerFeedbackDelegate: NSObject, UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
if let viewController = viewController as? NavigationControllerFeedback {
viewController.willShow?()
}
}
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
if let viewController = viewController as? NavigationControllerFeedback {
viewController.didShow?()
}
}
}
/*
HOWTO:
Set this object as the delegate on your Navigation View Controller,
final class foo {
fileprivate let navigationDelegate = UINavigationControllerDelegate()
{
self.navigationController.delegate = self.navigationDelegate
}
}
And in your view controllers:
final class FooViewController: UIViewController, NavigationControllerFeedback {
// optional
func willShow() {
self.doStuff()
}
// optional
func didShow() {
self.doOtherStuff()
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment