Giter VIP home page Giter VIP logo

router's Introduction

HOW TO USE

make your ViewController implement protocol Routable

extension FirstViewController: Routable {
    static func initWithParams(params: RouterParam?) -> UIViewController? {
        let sb = UIStoryboard.init(name: "Main", bundle: nil)
        let vc = sb.instantiateViewControllerWithIdentifier("FirstViewController") as! FirstViewController
        vc.hidesBottomBarWhenPushed = true
        
        if let params = params {
            let converter = RouterParamsConverter(params: params)
            converter.valueWithKey(kStringKey, out: &vc.name)
            converter.valueWithKey(kIntKey, out: &vc.age)
            converter.valueWithKey(kEnumKey, out: &vc.week)
            converter.valueWithKey(kClassKey, out: &vc.classModel)
            converter.valueWithKey(kStructKey, out: &vc.structModel)
            converter.valueWithKey(kClosureKey, out: &vc.closure)
            
            print("got params String name: \(vc.name)\n Int age : \(vc.age)\n Enum week: \(vc.week)\n Class: \(vc.classModel?.description)\n Struct: \(vc.structModel)\n")
            let result = vc.closure?(name: "Lily", age: 10)
            print("closure result: \(result)")
        }
        
        return vc
    }
    
    static var routableKey: String {
        return "FirstViewController"
    }
}

call map before you route your viewController

  let router = Router.sharedInstance
  router.navigationController = nav
  router.map(FirstViewController.routableKey, className: FirstViewController.description())      router.map(SecondViewController.routableKey, className: SecondViewController.description())
  router.map(WebViewController.routableKey, className: WebViewController.description())      router.map(FirstNavigationController.routableKey, className: FirstNavigationController.description())

call open to route your viewController

Router.sharedInstance.open(FirstViewController.routableKey)
///got the vc just inited
 Router.sharedInstance.open(WebViewController.routableKey, animated: true) { (opened) in
    if let opened = opened as? WebViewController {
    	///
    }
  }

route with params RouterParam

let params: RouterParam = [FirstViewController.kStringKey: "zhongan",
                            FirstViewController.kIntKey: 3,
                            FirstViewController.kEnumKey: Week.Monday,
                            FirstViewController.kClassKey: ClassModel(name: "china"),
                            FirstViewController.kStructKey: StructModel(name: "us"),
                            FirstViewController.kClosureKey: closure]
        
Router.sharedInstance.open(FirstViewController.routableKey, params: params)

route with options RouterOptions

let option = RouterOptions(presentationStyle: .Popover, transitionStyle: .FlipHorizontal, isModal: true)
Router.sharedInstance.open(FirstNavigationController.routableKey, options: option)

pop your viewController

Router.sharedInstance.pop()
///pop to root
Router.sharedInstance.pop(toRoot:true, animated: true)

special viewController like UIAlertController

let param = UIAlertController.alertParams(nil, message: "message", preferredStyle: .Alert)
Router.sharedInstance.open(UIAlertController.routableKey, params: param, options: UIAlertController.defaultRouterOptions, animated: true) { (opened) in
    if let alertController = opened as? UIAlertController {
        let okAction = UIAlertAction(title: "ok", style: .Default, handler: { (alertAction) in
            print("clicked ok")
        })
        
        alertController.addAction(okAction)
    }
}

TODO

  • more special ViewController(UIImagePickerController ...)
  • more test

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.