Latest 2.3.0
License MIT
Platforms ios 8.0
Frameworks Foundation, UIKit

MK2Router is a view controller routing utility for Swift.

See also: Qiita(Japanese)


  • iOS 8.0+
  • Xcode 8.0+
  • Swift 3.0



Add the following line to your Cartfile.

github 'imk2o/MK2Router' ~> 2.2.0

Run carthage update to build the framework and drag the built MK2Router.framework into your Xcode project.


Add the following line to your Podfile.

pod 'MK2Router', '~> 2.2.0'

Run pod install and open your Xcode workspace.


The DestinationType protocol

The DestinationType protocol indicates that the routable destination view controller.
Declare typealias Context that the required parameter type for routing.

class ItemDetailViewController: UIViewController, DestinationType {
  typealias Context = Int


The UIViewController#context property refers to passed parameters.

override func viewDidLoad() {

    let itemID: Int = self.context

Routing with segue

The UIStoryboardSegue extension provides passing parameters between view controllers.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    segue.mk2.context(ifIdentifierEquals: "ShowDetail") { (destination: ItemDetailViewController) in
            let indexPath = self.tableView.indexPathForSelectedRow,
            let selectedItem = self.items?[indexPath.row]
        else {

        return selectedItem.ID

Manual routing

The Router class provides some instance methods.
The following example is present a ItemDetailViewController that instantiates with Main storyboard’s ItemDetailNav layout.

    storyboardName: "Main",
    storyboardID: "ItemDetailNav"
) { (destination: ItemDetailViewController) -> Int in
    return itemID    // pass the seleted item ID

Variant Context

If you want the DestinationType.Context equips variant type, use enum type as context.

class ItemDetailViewController: UIViewController, DestinationType {
    enum ContextType {
        case itemID(Int)
        case item(Item)
    typealias Context = ContextType


Feedback with unwind segue

If you want to feedback values to source view controller using unwind segue, do as follows.

  • Store the feedback values when unwind (in destination view controller)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { "Unwind") { (source: SearchOptionViewController) in
        return self.keywordTextField.text ?? ""
  • Get the feedback values (in source view controller)
@IBAction func unwindFromSearchOption(_ segue: UIStoryboardSegue) {
    if let keyword = SearchOptionViewController.self) {
        self.loadItems(keyword: keyword)


The MIT License.

Latest podspec

    "name": "MK2Router",
    "version": "2.3.0",
    "summary": "Routing utility for UIViewController",
    "description": "MK2Router is a routing utility for UIViewController using Swift.",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": ""
    "authors": {
        "Yuichi Kobayashi": "[email protected]"
    "platforms": {
        "ios": "8.0"
    "source": {
        "git": "",
        "tag": "2.3.0"
    "source_files": "MK2Router-iOS/*.swift",
    "frameworks": [
    "pushed_with_swift_version": "3.0"

Pin It on Pinterest

Share This