简体   繁体   中英

How to dismiss PopViewController for another ViewController

I'm using ashleymills/Reachability to check if user is connected to the internet and if not I want to show a popup and if there was an internet connection I want the popup to disappear. the first part was easy and I was able to do , but I couldn't dismiss the popup when there is internet connection

The main view:

 func checkConnection() {
        let VC = self.getStoryBoard().instantiateViewController(withIdentifier: "checkConnection") as! checkConnection
        VC.checkConnectivity = self
        self.showPopUp(VC, parent: self)
    }
       func isConnect(){
     let VC = self.getStoryBoard().instantiateViewController(withIdentifier: "checkConnection") as! checkConnection
    VC.checknotConnectivity = self
    VC.dismissVC()
}

    @objc func reachabilityChanged(note: Notification) {
        let reachability = note.object as! Reachability
        switch reachability.connection {
        case .wifi:
           isConnect()
        case .cellular:
            isConnect()
        case .none:
         checkConnection()
        }
    }

the popup view

var checkConnectivity : checkConnectivity?
 func dismissVC() {
       // self.dismiss(animated: false, completion: nil)
        print("dismissVC")
    }

protocol checkConnectivity {
    func isConnect()
    func checkConnection()
}

I could see the print in the debugger which means my code is being read , but does not dismiss.

It is because you're initialising a controller in checkConnection method and popping up but in isConnect method you're again initialising the controller which is making a different instance of same controller and hence the previously initialised controller is not getting dismissed.

Try making the controller in checkConnection method as global and dismiss that controller in isConnect method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM