Skip to content

You can pass the context to destination view controller easily

License

Notifications You must be signed in to change notification settings

tokorom/SegueContext

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

97 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SegueContext

Swift Version License Cocoapods Compatible Carthage Compatible

You can pass the context to destination view controller easily.

send-context

This is new version of TKRSegueOptions for Swift!

Do you have no complaints on the view controller transition now?

  • Tight coupling!
  • That's a bother...

This is a bother code...

self.performSegue(withIdentifier: "Next", sender: nil)

// and

override func prepareForSegue(segue: UIStoryboardSegue, sender sender: AnyObject?) {
    switch segue.identifier {
    case "Next":
        if let nextViewController = segue.destinationViewController as? NextViewController {
            nextViewController.value = 10
            nextViewController.delegate = self
        }
    case "OtherWithNavi":
        if let navigationController = segue.destinationViewController as? UINavigationController {
            if let nextViewController = navigationController.viewControllers.first as? NextViewController {
              nextViewController.value = 20
            }
        }
    default:
        break
    }
}

// and

extension MyViewController: NextViewControllerDelegate {
    override func itemDidSelect(item: Item) {
        // get an item!
    }
}

SegueContext will solve these problems!

This is a new simple code with SegueContext! yeah!

self.performSegue(withIdentifier: "Next", context: 10) { (item: Item) -> Void in
    // get an item!
}

Simple Usage

When you want to send a context to the destination ViewController

  • Source View Controller
self.performSegue(withIdentifier: "Next", context: 10)
  • Destination View Controller
if let value: Int = self.contextValue() {
    self.value = value
}

When you need callback from destination ViewController

  • Source View Controller
self.performSegue(withIdentifier: "Next", context: 10) { (item: Item) -> Void in
    // get an item!
}
  • Destination View Controller
if let callback: (Item) -> Void = self.callback() {
    callback(selectedItem)
}

When you need Container View Controller

  • Sample 1: manually
if let viewController = self.childViewControllers.first as? XXX {
    viewController.sendContext(10)
}
  • Sample 2: use prepareForSegue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    self.contextSenderForSegue(segue) { segueIdentifier, viewController, sendContext in
        switch segueIdentifier {
        case "Embedded1", "Embedded2":
            sendContext(10)
        default:
            break
        }
    }
}

Other Usages

performSegue

// normal
self.performSegue(withIdentifier: "Next", context: 10)

// with callback
self.performSegue(withIdentifier: "Next", context: 10) { (item: YourItem) -> Void in
    // get a your item!
}

presentViewController

self.present(storyboardName: "xxx", animated: true, context: 10)
self.present(storyboardName: "xxx", identifier: "xxx", animated: true, context: 10)
self.present(storyboardName: "xxx", animated: true, context: 10)
self.present(storyboardName: storyboard, animated: true, context: 10)

self.present(storyboardName: "xxx", context: 10) { (item: YourItem) -> Void in
    // get a your item!
}

pushViewController

self.pushViewController(storyboardName: "xxx", animated: true, context: 10)
self.pushViewController(storyboardName: "xxx", identifier: "xxx", animated: true, context: 10)
self.pushViewController(storyboardName: "xxx", animated: true, context: 10)
self.pushViewController(storyboard: storyboard, animated: true, context: 10)

// with callback
self.pushViewController(storyboardName: "xxx", context: 10) { (item: YourItem) -> Void in
    // get a your item!
}

Manually send the context

viewController.sendContext(10)
let vc = UIViewController.viewController(storyboardName: "xxx", context: 10) as? MyViewController
let vc = UIViewController.viewController(storyboardName: "xxx", identifier: "xxx", context: 10) as? MyViewController
let vc = UIViewController.viewController(storyboard: storyboard, context: 10) as? MyViewController

Additional good news!

  • SegueContext sends automatically the context to rootViewController of UINavigationController
  • SegueContext sends automatically the context to viewControllers of UITabBarController

send-context

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects.

You can install it with the following command:

$ gem install cocoapods

To integrate SegueContext into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'SegueContext'

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate SegueContext into your Xcode project using Carthage, specify it in your Cartfile:

github "tokorom/SegueContext"

Then, run the following command:

$ carthage update

Then, link your app with SegueContext.framework.