SegueCoordinator

open class SegueCoordinator

Builds on top of the storyboard and encapsulates navigation logic. View controllers delegates navigation requests to it and SegueCoordinator performs all required transitions as well as data transfer. Typically you should use its subclasses instead.

  • Reference to the storyboard the SegueCoordinator work with.

    Declaration

    Swift

    public let storyboard: UIStoryboard
  • Root navigation controller that was passed into initializer of SegueCoordinator.

    Declaration

    Swift

    public let rootNavigationController: UINavigationController
  • Typically, you should override this initializer and specify storyboard name.

    Example:

    init(navController: UINavigationController) {
       super.init(storyboardName: "Main", rootNavigationController: navController)
    }
    

    Declaration

    Swift

    public init(storyboardName: String, rootNavigationController: UINavigationController)

    Parameters

    storyboardName

    Name of the storyboard the coordinator will work with.

    rootNavigationController

    Navigation controller for displaying controllers created by coordinator.

  • Pops view controller from the stack of the current navigation controller.

    Warning

    If the top view controller doesn’t wrapped in navigation controller the pop action will do nothing.

    Declaration

    Swift

    func pop(animated: Bool = true)

    Parameters

    animated

    Specify true to animate the transition. Otherwise, specify false.

  • Dismiss current modal view controller.

    Warning

    If the top view controller doesn’t present modally the closeModal action will do nothing.

    Declaration

    Swift

    func closeModal(animated: Bool = true, completion: (() -> Void)? = nil)

    Parameters

    animated

    Specify true to animate the transition. Otherwise, specify false.

    completion

    The block to execute after the view controller is dismissed.

  • This method will make a transition to the farthest matching controller. It performs searching for the first view controller matching the specified type. Then it closes all modal view controllers above the found view controller’s stack and pops navigation stack to make the found view controller the top. In other words, the found view controller becomes the top and visible.

    Declaration

    Swift

    func unwindToFirst<T>(type: T.Type, animated: Bool = true, completion: (() -> Void)? = nil) where T : UIViewController

    Parameters

    type

    Type of required view controller.

    animated

    specify true if you want to animate the transition. Specify false otherwise.

    completion

    The block to execute after unwind transitions are finished.

  • This method will make a transition to the closest matching controller. It performs searching for the last view controller matching the specified type. Then it closes all modal view controllers above the found view controller’s stack and pops navigation stack to make the found view controller the top. In other words, the found view controller becomes the top and visible.

    Declaration

    Swift

    func unwindToLast<T>(type: T.Type, animated: Bool = true, completion: (() -> Void)? = nil) where T : UIViewController

    Parameters

    type

    Type of required view controller.

    animated

    specify true if you want to animate the transition. Specify false otherwise.

    completion

    The block to execute after unwind transitions are finished.

  • This method will make a transition to the farthest matching controller. It performs searching for the first view controller matching the specified predicate. Then it closes all modal view controllers above the found view controller’s stack and pops navigation stack to make the found view controller the top. In other words, the found view controller becomes the top and visible.

    Declaration

    Swift

    func unwindToFirst(animated: Bool = true, where predicate: (UIViewController) -> Bool, completion: (() -> Void)? = nil)

    Parameters

    animated

    specify true if you want to animate the transition. Specify false otherwise.

    predicate

    Searching conditions. Returns true if the view controller passed in its argument fulfills conditions of the search. Returns false otherwise.

    controller

    Controller to check for the searching conditions.

    completion

    The block to execute after unwind transitions are finished.

  • This method will make a transition to the closest matching controller. It performs searching for the last view controller matching the specified predicate. Then it closes all modal view controllers above the found view controller’s stack and pops navigation stack to make the found view controller the top. In other words, the found view controller becomes the top and visible.

    Declaration

    Swift

    func unwindToLast(animated: Bool = true, where predicate: (_ controller: UIViewController) -> Bool, completion: (() -> Void)? = nil)

    Parameters

    animated

    specify true if you want to animate the transition. Specify false otherwise.

    predicate

    Searching conditions. Returns true if the view controller passed in its argument fulfills conditions of the search. Returns false otherwise.

    controller

    Controller to check for the searching conditions.

    completion

    The block to execute after unwind transitions are finished.

  • Closes all modal view controllers above the specified view controller’s stack and pops navigation stack to make it the top. In other words, the specified view controller becomes the top and visible.

    Declaration

    Swift

    func unwindToController(_ controller: UIViewController, animated: Bool = true, completion: (() -> Void)? = nil)

    Parameters

    controller

    controller to navigate to

    animated

    specify true if you want to animate the transition. Specify false otherwise.

    completion

    The block to execute after unwind transitions are finished.

  • Create initial view controller from storyboard.

    Declaration

    Swift

    func instantiateInitial<T>(type: T.Type) -> T where T : UIViewController

    Parameters

    type

    Expected type of view controller.

  • Create view controller from storyboard by identifier.

    Declaration

    Swift

    func instantiate<T>(_ storyboardId: String, type: T.Type) -> T where T : UIViewController

    Parameters

    storyboardId

    Storyboard ID of view controller specified with identity inspector in Interface Builder.

    type

    expected Type of view controller.

  • Create the initial view controller from the storyboard and present it modally from the current navigation controller.

    Declaration

    Swift

    func modalInitial<T>(type: T.Type, style: UIModalPresentationStyle, wrapInNavigationController: Bool = true, animated: Bool = true, prepareController: (_ controller: T) -> Void) where T : UIViewController

    Parameters

    type

    Expected type of view controller.

    style

    Modal presentation style.

    wrapInNavigationController

    Specify true if you want to wrap modal controller in it’s own navigation controller. Otherwise, specify false.

    animated

    Specify true to animate the transition. Otherwise, specify false.

    prepareController

    The block to execute after controller will be created. Pass any data and dependencies into view controller here.

    controller

    Created view controller

  • Create the view controller from the storyboard by identifier and present it modally from the current navigation controller.

    Declaration

    Swift

    func modal<T>(_ storyboardId: String, type: T.Type, style: UIModalPresentationStyle, wrapInNavigationController: Bool = true, animated: Bool = true, prepareController: (_ controller: T) -> Void) where T : UIViewController

    Parameters

    storyboardId

    Storyboard ID of view controller specified with identity inspector in Interface Builder.

    type

    Expected type of view controller.

    style

    Modal presentation style.

    wrapInNavigationController

    Specify true if you want to wrap modal controller in it’s own navigation controller. Otherwise, specify false.

    animated

    Specify true to animate the transition. Otherwise, specify false.

    prepareController

    The block to execute after controller will be created. Pass any data and dependencies into view controller here.

    controller

    Created view controller

  • Present controller modally from the current navigation controller.

    Declaration

    Swift

    func modalController<T>(_ controller: T, style: UIModalPresentationStyle, wrapInNavigationController: Bool = true, animated: Bool = true, prepareController: (_ controller: T) -> Void) where T : UIViewController

    Parameters

    controller

    Controller to be presented.

    style

    Modal presentation style.

    wrapInNavigationController

    Specify true if you want to wrap modal controller in it’s own navigation controller. Otherwise, specify false.

    animated

    Specify true to animate the transition. Otherwise, specify false.

    prepareController

    The block to execute before controller will be presented. Pass any data and dependencies into view controller here.

    controller

    Controller to be presented.

  • Create the initial view controller from the storyboard and push it to the stack of the current navigation controller.

    Warning

    If the top view controller doesn’t wrapped in navigation controller the push action will do nothing.

    Declaration

    Swift

    func pushInitial<T>(type: T.Type, animated: Bool = true, prepareController: (_ controller: T) -> Void) where T : UIViewController

    Parameters

    type

    Expected type of view controller.

    animated

    Specify true to animate the transition. Otherwise, specify false.

    prepareController

    The block to execute after controller will be created. Pass any data and dependencies into view controller here.

    controller

    Created view controller

  • Create the view controller from the storyboard by identifier and push it to the stack of the current navigation controller.

    Warning

    If the top view controller doesn’t wrapped in navigation controller the push action will do nothing.

    Declaration

    Swift

    func push<T>(_ storyboardId: String, type: T.Type, animated: Bool = true, prepareController: (T) -> Void) where T : UIViewController

    Parameters

    storyboardId

    Storyboard ID of view controller specified with identity inspector in Interface Builder.

    type

    Expected type of view controller.

    animated

    Specify true to animate the transition. Otherwise, specify false.

    prepareController

    The block to execute after controller will be created. Pass any data and dependencies into view controller here.

    controller

    Created view controller

  • Push view controller to the stack of the current navigation controller.

    Warning

    If the top view controller doesn’t wrapped in navigation controller the push action will do nothing.

    Declaration

    Swift

    func pushController<T>(_ controller: T, animated: Bool = true, prepareController: (_ controller: T) -> Void) where T : UIViewController

    Parameters

    controller

    Controller to be presented.

    animated

    Specify true to animate the transition. Otherwise, specify false.

    prepareController

    The block to execute before controller will be presented. Pass any data and dependencies into view controller here.

    controller

    Controller to be presented.

  • This method will search the controller by its type. Search for view controller is starting on the navigation stack of the root navigation controller. Then the search continues on the navigation stack of the presented view controller and so on. This method will return the first view controller that will fulfill the conditions of the search. In other words, it returns the farthest to the current view controller in the navigation stack.

    Declaration

    Swift

    func findFirst<T>(type: T.Type) -> T? where T : UIViewController

    Parameters

    type

    Type of required view controller.

    Return Value

    view controller if it was found, nil otherwise.

  • This method will search the controller with custom conditions declared in the predicate. Search for view controller is starting on the navigation stack of the root navigation controller. Then the search continues on the navigation stack of the presented view controller and so on. This method will return the first view controller that will fulfill the conditions of the search. In other words, it returns the farthest to the current view controller in the navigation stack.

    Declaration

    Swift

    func findFirst(where predicate: (_ controller: UIViewController) -> Bool) -> UIViewController?

    Parameters

    predicate

    Searching conditions. Returns true if the view controller passed in its argument fulfills conditions of the search. Returns false otherwise.

    controller

    Controller to check for the searching conditions.

    Return Value

    view controller if it was found, nil otherwise.

  • This method will search the controller by its type. Search for view controller starting on the navigation stack of the root navigation controller. Search for view controller is starting on the navigation stack of the root navigation controller. Then the search continues on the navigation stack of the presented view controller and so on. This method will return the last view controller that will fulfill the conditions of the search. In other words, it returns the nearest to the current view controller in the navigation stack.

    Declaration

    Swift

    func findLast<T>(type: T.Type) -> T? where T : UIViewController

    Parameters

    type

    Type of required view controller.

    Return Value

    view controller if it was found, nil otherwise.

  • This method will search the controller with custom conditions declared in the predicate. Search for view controller is starting on the navigation stack of the root navigation controller. Then the search continues on the navigation stack of the presented view controller and so on. This method will return the last view controller that will fulfill the conditions of the search. In other words, it returns the nearest to the current view controller in the navigation stack.

    Declaration

    Swift

    func findLast(where predicate: (_ controller: UIViewController) -> Bool) -> UIViewController?

    Parameters

    predicate

    Searching conditions. Returns true if the view controller passed in its argument fulfills conditions of the search. Returns false otherwise.

    controller

    Controller to check for the searching conditions.

    Return Value

    view controller if it was found, nil otherwise.

  • Perform segue from currently presented view controller

    Warning

    If you override prepareForSegue method, be sure to call super.prepareForSegue

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)
    }
    

    Declaration

    Swift

    func segue<T: UIViewController>(_ segueId: String, type: T.Type, prepareController: @escaping (_ controller: T) -> Void)

    Parameters

    segueId

    Segue Identifier specified with Attributes Inspector in Interface Builder

    type

    Expected type of destination view controller.

    prepareController

    The block to execute before controller will be presented. This is prepareForSegue replacement. Pass any data and dependencies into view controller here.

    controller

    Controller to be presented.

  • Create the initial view controller from the storyboard and make it the root of the rop navigation controller. All other presented controllers will be removed from navigation stack.

    Warning

    If the top view controller doesn’t wrapped in navigation controller the set action will do nothing.

    Declaration

    Swift

    func setInitial<T>(type: T.Type, prepareController: (_ controller: T) -> Void) where T : UIViewController

    Parameters

    type

    Expected type of view controller.

    prepareController

    The block to execute after controller will be created. Pass any data and dependencies into view controller here.

    controller

    Created view controller

  • Create the view controller from the storyboard by identifier and make it the root of the top navigation controller. All other presented controllers will be removed from navigation stack.

    Warning

    If the top view controller doesn’t wrapped in navigation controller the set action will do nothing.

    Declaration

    Swift

    func set<T>(_ storyboardId: String, type: T.Type, prepareController: (_ controller: T) -> Void) where T : UIViewController

    Parameters

    storyboardId

    Storyboard ID of view controller specified with identity inspector in Interface Builder.

    type

    Expected type of view controller.

    prepareController

    The block to execute after controller will be created. Pass any data and dependencies into view controller here.

    controller

    Created view controller

  • Make view controller the root of the top navigation controller. All other presented controllers will be removed from navigation stack.

    Warning

    If the top view controller doesn’t wrapped in navigation controller the set action will do nothing.

    Declaration

    Swift

    func setController<T>(_ controller: T, prepareController: (_ controller: T) -> Void) where T : UIViewController

    Parameters

    controller

    Controller to be presented

    prepareController

    The block to execute before controller will be presented. Pass any data and dependencies into view controller here.

    controller

    Controller to be presented

  • Returns top view controller.

    Declaration

    Swift

    var topController: UIViewController { get }
  • Returns navigation controller of top view controller if it exists. Returns nil otherwise.

    Declaration

    Swift

    var topNavigationController: UINavigationController? { get }