swiftswift-protocolspushviewcontrollerpresentviewcontroller

How to push controller after dismiss presented controller in swift?


Hey I am showing a controller (A) in main controller with presentation style (not push), and I want to button tapped and push another controller (B) after dismiss this (A) controller, this situation occurred in main controller. I am using protocol for this situation. Any idea for that ? Code like below.`

//this is dismiss button action

   var segueDelegate: segueFromController?

   @objc func dismissController() {

    self.dismiss(animated: true) {
        self.segueDelegate?.segueFromController()
    }

//and this one is protocol function in main controller


    func segueFromController() {

     let contProfile = ContViewController(collectionViewLayout: UICollectionViewFlowLayout())

           navigationController?.pushViewController(contProfile, animated: true)

}

// and I am making "self" this protocol in main controller's didload

  let aCont = AController()

  override func viewDidLoad() {
            super.viewDidLoad()

            AController.segueDelegate = self
 }

 // protocol

protocol segueFromController { 
  func segueFromController()  
  }

// this is presenting (A) controller code in main page

func openController() {
            let preController = AController()
        preController.modalPresentationStyle = .fullScreen
        self.present(preController, animated: true, completion: nil)

}

Solution

  • First you need to make this segueDelegate weak

    protocol segueFromController : class {
    func segueFromController()
    }
    weak var segueDelegate: segueFromController?
    
    func openController() {
                let preController = AController()
             preController.segueDelegate = self
            preController.modalPresentationStyle = .fullScreen
            self.present(preController, animated: true, completion: nil)
    
    }
    

    Try to dismiss without animation

    self.dismiss(animated: false) {
                 self.segueDelegate?.segueFromController()
            }