How to close controllerView on button click?

I use navigation.pushViewController to open a ViewController, the opened Storyboard has a close button, I use navigation.popViewController to close it, but it doesn’t work neither it shows an error, full code below:

import UIKit

class ConfirmKycViewController: UIViewController {
    private unowned let navigation: UINavigationController? = nil
    override func viewDidLoad() {

        // Do any additional setup after loading the view.
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        super.prepare(for: segue, sender: sender)
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    @IBAction func CloseViewController(_ sender: Any) {
        navigation?.popViewController(animated: true)
        dismiss(animated: true, completion: nil)


Thank you for help

>Solution :

You just need to change navigation?.popViewController(animated: true) with self.navigationController?.popViewController(animated: true)

Leave a Reply