Created
May 4, 2018 17:29
-
-
Save AnsonT/4b24e604380399de57e53c9ebd81d82c to your computer and use it in GitHub Desktop.
[Custom Eureka Push Row in Swift] Use a custom view controller for a 'push row' #eureka #swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CustomPresenterRow.swift | |
// LinkedNow | |
// | |
// Created by Anson Tsao on 5/3/18. | |
// Copyright © 2018 Anson Tsao. All rights reserved. | |
// | |
import Foundation | |
import Eureka | |
public final class CustomPresenterRow<ControllerType: TypedRowControllerType & UIViewController>: OptionsRow<PushSelectorCell<ControllerType.RowValue>>, PresenterRowType, RowType { | |
public typealias PresenterRow = ControllerType | |
public typealias RowValue = ControllerType.RowValue | |
open var presentationMode: PresentationMode<PresenterRow>? | |
/// Will be called before the presentation occurs. | |
open var onPresentCallback: ((FormViewController, PresenterRow) -> Void)? | |
var storyBoardName: String? | |
var controllerName: String? | |
public required init(tag: String?) { | |
super.init(tag: tag) | |
presentationMode = .show(controllerProvider: ControllerProvider.callback { | |
if let storyboardName = self.storyBoardName, | |
let controllerName = self.controllerName { | |
let storyboard = UIStoryboard(name: storyboardName, bundle: nil) | |
return storyboard.instantiateViewController(withIdentifier: controllerName) as! ControllerType | |
} else { | |
return ControllerType() | |
} | |
}, | |
onDismiss: { vc in _ = vc.navigationController?.popViewController(animated: true) }) | |
} | |
open override func customDidSelect() { | |
super.customDidSelect() | |
guard let presentationMode = presentationMode, !isDisabled else { return } | |
if let controller = presentationMode.makeController() { | |
controller.row = self | |
controller.title = selectorTitle ?? controller.title | |
onPresentCallback?(cell.formViewController()!, controller) | |
presentationMode.present(controller, row: self, presentingController: self.cell.formViewController()!) | |
} else { | |
presentationMode.present(nil, row: self, presentingController: self.cell.formViewController()!) | |
} | |
} | |
open override func prepare(for segue: UIStoryboardSegue) { | |
super.prepare(for: segue) | |
guard let rowVC = segue.destination as? PresenterRow else { return } | |
rowVC.title = selectorTitle ?? rowVC.title | |
rowVC.onDismissCallback = presentationMode?.onDismissCallback ?? rowVC.onDismissCallback | |
onPresentCallback?(cell.formViewController()!, rowVC) | |
rowVC.row = self | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyCustomRowViewController: UIViewController, TypedRowControllerType { | |
public var row: RowOf<String>! | |
public var onDismissCallback: ((UIViewController) -> Void)? | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension MyFormViewController { | |
private func setupForm() { | |
form | |
+++ Section() | |
<<< CustomPresenterRow<MyCustomRowViewController>() { row in | |
row.storyBoardName = "Main" | |
row.controllerName = "MyCustomRowViewController" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment