I would recommend creating a delegate to reference the view controller you wish to dismiss:
protocol ViewDelegate {
func dismissViewController()
}
Then make your view controller inherit this delegate and make sure to create a dismissViewController func to conform to the protocol:
class ViewController: UIViewController, ViewDelegate {
func dismissViewController() {
self.dismiss(animated: true, completion: nil)
}
}
Lastly, pass a reference of this delegate to your view and use the dismissViewController func:
class CustomView: UIView {
var delegate: ViewDelegate?
@objc func cancelButtonTapped() {
delegate.dismissViewController()
}
}