Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pop a navigation controller

Tags:

ios

swift

I have two views, V1 and V2. I want to "present" V2 when the add button is pressed on V1, and "pop" V2 off when the stop button is pressed, so that the original V1 is the top of the stack.

From what I have read, I need a separate view controller for V2. From the limited information I could find, I need V1's view controller to conform to V2's protocol, V2delegate. This is what I have, but it is not working:

ViewController1 with V1

class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout, FormViewControllerDelegate {

    let form = FormViewController()

    func addTapped() {
        form.delegate = self
        let nav = UINavigationController(rootViewController: form)

        navigationController?.present(nav, animated: true)
    }

    func popForm() {
        navigationController?.popViewController(animated: true)
        navigationController?.popToViewController(self, animated: true)
        print("popped")
    }
}

enter image description here

ViewController2 with V2

class FormViewController: UIViewController {

    var delegate: FormViewControllerDelegate?

    func stopTapped() {
        print("pop it")
        delegate?.popForm()
    }
}

protocol FormViewControllerDelegate {
    func popForm()
}

enter image description here

What am I doing wrong here?

like image 960
defoification Avatar asked Dec 31 '25 10:12

defoification


1 Answers

In your VC2, Change to use this code

func stopTapped() {
    print("pop it")
    self.dismiss(animated: true, completion: nil)
}
like image 98
Fangming Avatar answered Jan 03 '26 03:01

Fangming



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!