Skip to content

Instantly share code, notes, and snippets.

@tobitech
Created November 28, 2018 11:55
Show Gist options
  • Save tobitech/1c3fb821e5f57ea8c2917c1e3c9f8f71 to your computer and use it in GitHub Desktop.
Save tobitech/1c3fb821e5f57ea8c2917c1e3c9f8f71 to your computer and use it in GitHub Desktop.
Simple flow for pushing another view controller on modal dismiss
protocol ModalViewControllerDelegate {
func pushToAnotherScreen()
}
class ModalViewController: UIViewController {
// MARK: - Properties
weak var delegate: ModalViewControllerDelegate
lazy var cancelButton: UIButton = {
let button = UIButton()
button.title = "Cancel"
return button
}()
// MARK: - View Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
button.addTarget(self, action: #selector(handleCancel), for: .touchUpInside)
}
@objc func handleCancel() {
dismiss(animated: true) {
self.delegate.pushToAnotherScreen()
}
}
}
class PresentingViewController: UIViewController, ModalViewControllerDelegate {
// MARK: - Properties
lazy var button: UIButton = {
let button = UIButton()
button.title = "Show Modal"
return button
}()
// MARK: - View Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
button.addTarget(self, action: #selector(handleShowModal), for: .touchUpInside)
}
@objc func handleShowModal() {
let modalViewController = ModalViewController
modalViewController.delegate = self
self.present(modalViewController, animated: true, completion: nil)
}
// MARK: - Modal View Controller Delegate.
func pushToAnotherScreen() {
// implementation to push to another screen
let newViewController = UIViewController()
self.navigationController?.pushViewController(newViewController, animated: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment