You can pass the context to destination view controller easily.
This is new version of TKRSegueOptions for Swift!
- Tight coupling!
- That's a bother...
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!
self.performSegue(withIdentifier: "Next", context: 10) { (item: Item) -> Void in
// get an item!
}
- Source View Controller
self.performSegue(withIdentifier: "Next", context: 10)
- Destination View Controller
if let value: Int = self.contextValue() {
self.value = value
}
- 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)
}
- 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
}
}
}
// normal
self.performSegue(withIdentifier: "Next", context: 10)
// with callback
self.performSegue(withIdentifier: "Next", context: 10) { (item: YourItem) -> Void in
// get a your item!
}
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!
}
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!
}
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
- SegueContext sends automatically the context to rootViewController of UINavigationController
- SegueContext sends automatically the context to viewControllers of UITabBarController
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 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
.