Declare Segue without storyboard. Use syntax of generic URIs.
- Centralize all your navigation.
- Use generic URIs to define your route.
- Don't loose time with crash due to missing character, use enum instead.
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods
To integrate Router into your Xcode project using CocoaPods, it in your Podfile
:
platform :ios, '9.0'
use_frameworks!
target '<Your Target Name>' do
pod 'Router', :git => 'https://github.com/favret/Router.git', :tag => '1.0.2'
end
Then, run the following command:
$ pod install
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate Router into your Xcode project using Carthage, specify it in your Cartfile
:
github "favret/Router" => 1.0.2
Run carthage update
to build the framework and drag the built Router.framework
into your Xcode project.
If you prefer not to use either of the aforementioned dependency managers, you can integrate Router into your project manually as an Embedded Framework.
public class MyRouter: Router {
. . .
}
First, you have to create a class who inherit Router
.
enum Routes: String {
case MyRoute = "route://Main/myViewController#modal"
case SecondRoute = "route://Main/secondViewController#push"
case ThirdRoute = "route://Main/thirdViewController#modal"
}
In your class MyRouter
, define all your route.
You can see in the above exemple that the uri is decompose like that :
host
, is the name of the storyboardpath
, is the viewController identifierfragment
, is the navigation's type of the route.
class SomeViewController: UIViewController {
. . .
MyRouter.perform(.MyRoute, sender: self)
. . .
MyRouter.perform(.SecondRoute)
. . .
MyRouter.perform(.ThirdRoute, sender: self, segueClass: MyCustomSegue.self)
}
As you can see, you can perform a route in many way. Basically, the perform
methods looks like
func perform(_ url: String, sender: AnyObject? = nil, segueClass: UIStoryboardSegue.Type)
url
, can ben anURL
,String
orRouteType
sender
, if you don't specify it, then the sender will be the first visible ViewControllersegueClass
, if you don't specify it, then the segueClass will beUIStoryboardSegue.self
import Foundation
import Router
public class MyRouter: Router {
enum Routes: String {
case MyRoute = "route://Main/myViewController#modal"
case SecondRoute = "route://Main/secondViewController#push"
case ThirdRoute = "route://Main/thirdViewController#modal"
}
public func perform(route: Routes, sender: AnyObject? = nil) {
self.perform(route.rawValue, sender: sender)
}
}
. . .
class SomeViewController: UIViewController {
func action() {
MyRouter.perform(.MyRoute, sender: self)
}
}