diff --git a/JSONCreator_iOS/JSONCreator/AppDelegate.swift b/JSONCreator_iOS/JSONCreator/AppDelegate.swift index 9591a37..364d09f 100644 --- a/JSONCreator_iOS/JSONCreator/AppDelegate.swift +++ b/JSONCreator_iOS/JSONCreator/AppDelegate.swift @@ -13,6 +13,10 @@ import MVMCoreUI class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate { var window: UIWindow? + var mvcNav: UINavigationController? + var dvcNav: UINavigationController? + var mvc: UIViewController? + var dvc: UIViewController? func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { // Setup our core object with the default implementation @@ -24,10 +28,26 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele // Override point for customization after application launch. let splitViewController = window!.rootViewController as! UISplitViewController - let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController - navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem + mvcNav = splitViewController.viewControllers[0] as? UINavigationController + mvcNav?.delegate = self + mvc = mvcNav?.topViewController + dvcNav = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as? UINavigationController + dvc = dvcNav?.topViewController + dvc?.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem + dvc?.navigationItem.leftItemsSupplementBackButton = true splitViewController.delegate = self splitViewController.preferredDisplayMode = .allVisible + + NotificationCenter.default.addObserver(forName: UIViewController.showDetailTargetDidChangeNotification, object: splitViewController, queue: nil) { [weak self] (notification) in + if let strongSelf = self, let svc = notification.object as? UISplitViewController { + if svc.isCollapsed { + strongSelf.mvc?.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Editor", style: .plain, target: self, action: #selector(self?.showEditor)) + } else { + strongSelf.mvcNav?.setViewControllers([strongSelf.mvc!], animated: false) + strongSelf.mvc?.navigationItem.rightBarButtonItem = nil + } + } + } return true } @@ -55,12 +75,22 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele // MARK: - Split view - /*func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController:UIViewController, onto primaryViewController:UIViewController) -> Bool { -// guard let secondaryAsNavController = secondaryViewController as? UINavigationController else { return false } -// guard let _ = secondaryAsNavController.topViewController as? DetailViewController else { return false } -// return false + func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController:UIViewController, onto primaryViewController:UIViewController) -> Bool { + //guard let secondaryAsNavController = secondaryViewController as? UINavigationController else { return false } + //guard let _ = secondaryAsNavController.topViewController as? DetailViewController else { return false } return true - }*/ - + } + + @objc func showEditor() { + mvcNav?.pushViewController(dvc!, animated: true) + } +} + +extension AppDelegate: UINavigationControllerDelegate { + func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) { + if mvc == viewController { + dvcNav?.setViewControllers([dvc!], animated: false) + } + } }