Created
November 28, 2018 11:55
-
-
Save tobitech/1c3fb821e5f57ea8c2917c1e3c9f8f71 to your computer and use it in GitHub Desktop.
Simple flow for pushing another view controller on modal dismiss
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
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() | |
} | |
} | |
} |
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 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