Direct
simplifies transitions between screens in iOS app.
- Copy content of
Source
folder to your project.
or
- Use
Direct
cocoapod
- iOS 9 and later
- Xcode 9 and later
- Swift 4
Create extension for Scene
class:
extension Scene {
static var main: Scene {
let navigationController = UINavigationController()
navigationController.viewControllers = [
MainViewController(nibName: "MainViewController", bundle: nil)
]
return Scene(rootController: navigationController)
}
}
Change AppDelegate.swift
:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Navigator.shared
.createWindow()
.setScene(.main)
return true
}
Also, remove reference to window (var window: UIWindow?
) from AppDelegate
class.
With Navigator
class you can create window in one line of code:
Navigator.shared.createWindow()
If you have a custom window class, it's possible to use it too:
Navigator.shared.createWindow(ofType: MyWindow.self)
It's recommended to use createWindow()
method in AppDelegate
(see example in Preparations section).
Usually, Xcode creates AppDelegate
class with a reference to UIWindow
inside:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
}
With Navigator
you don't need to keep this reference, so you can remove it from AppDelegate
class. Use Navigator.window
instead.
Scene describes navigation stack including root controller. Here's an example:
let navigationController = UINavigationController()
navigationController.viewControllers = [
MyViewController(nibName: "MyViewController", bundle: nil)
]
let scene = Scene(rootController: navigationController)
It's highly recommended to create extension for Scene
class and provide static variables describing new scenes like it's done in Preparations section.
Switching between scenes is simple:
Navigator.shared.setScene(newScene)
Transition is an action that is performed with stack of navigation controller, for example: pushing, presenting, dismissing view controllers, etc. Below you can find list of examples how to manage navigation stack with Direct
library.
Push view controller:
let someViewController = SomeViewController(nibName: "SomeViewController", bundle: nil)
Navigator.shared.performTransition(.push(viewController: someViewController, animated: true))
Pop:
Navigator.shared.performTransition(.pop(animated: true))
Pop to root view controller:
Navigator.shared.performTransition(.popToRootViewController(animated: true))
Present:
Navigator.shared.performTransition(.present(viewController: someViewController, animated: true, completion: {
}))
Dismiss:
Navigator.shared.performTransition(.dismiss(animated: true, completion: {
}))
Access to current navigation controller:
if let currentNavigationController = Navigator.shared.scene?.rootNavigationController {
// Do something with current navigation controller
}
Navigator
supports call chains so you can write long expressions:
Navigator.shared
.createWindow()
.setScene(.main)
.performTransition(.push(viewController: someViewController, animated: false))
.performTransition(.present(viewController: popupViewController, animated: true, completion: {
}))
Direct
is available under the MIT license. See the LICENSE file for more info.