1

I have 2 view controllers A and B. The view controller A present view controller B by segue show. ViewController B has also a button which dismiss B and show A. There is no problem until here.

After some functions completed in B, view controller B present a popup view and this popup include restart game and close game buttons. When press close game button, View Controller B and popup view should be dismissed and show main A view controller. How to accomplish this? Thank you

Here how to present popup view:

let popupVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "popupID") as! PopupViewController
        self.addChildViewController(popupVC)
        popupVC.view.frame = self.view.frame
        self.view.addSubview(popupVC.view)
        popupVC.didMove(toParentViewController: self)
Gokhan Turkben
  • 101
  • 1
  • 3
  • 9
  • Possible duplicate of [IOS - How to segue programmatically using swift](https://stackoverflow.com/questions/27604192/ios-how-to-segue-programmatically-using-swift) – Henry Jun 22 '17 at 13:33
  • Add your any attempt code and thing you have done to achieve. – dahiya_boy Jun 22 '17 at 13:37
  • Are you attempting for ipad or iPhone ? if ipad pop view means pop over controller used ? – Viral Narshana Jun 22 '17 at 14:10
  • both iPhone and iPad. But is is not popover view. just game over screen. – Gokhan Turkben Jun 22 '17 at 14:12
  • Are you aware about protocol ? write down protocol in PopupViewController and implement that method in class B. and in class B in that method write self.dismiss(animated: true, completion: nil). – Viral Narshana Jun 22 '17 at 14:20

2 Answers2

3

Here a solution in Swift3.

So as I understand you want to dismiss the popup presented in ViewController B and return back to ViewController A.

    let alertController = UIAlertController(title: "alert", message: "tara", preferredStyle: .alert)

    let action = UIAlertAction(title: "dismiss", style: .default) { (UIAlertAction) in

        // For Dismissing the Popup
        self.dismiss(animated: true, completion: nil)

        // Dismiss current Viewcontroller and back to ViewController B
        self.navigationController?.popViewController(animated: true)

    }
    alertController.addAction(action)
    self.present(alertController, animated: true, completion: nil)
Fares Benhamouda
  • 589
  • 8
  • 21
3

The conventional method to achieve this is using delegation. In your case, you have to first create a protocol for the delegate

protocol PopupViewControllerDelegate {
    func didSelectClose(_ popupVC: PopupViewController)
}

Now add a variable inside PopupViewController that will be used to call the delegate's methods

class PopupViewController: UIViewController {
    var delegate: PopupViewControllerDelegate?
}

When the user taps the close button in your popup, you should call the delegate's method to notify it of user's action.

func didClose(sender: Any) {
    delegate?.didSelectClose(self)
}

Now, you will have to implement PopupViewControllerDelegate inside ViewControllerB as follows:

class ViewControllerB: UIViewController, PopupViewControllerDelegate {
    func didSelectClose(_ popupVC: PopupViewController) {
        // dismiss and go to Root View Controller (A)
        dismiss(animated: true) { 
            self.navigationController?.popToRootViewController(animated: true)
        }
    }
}

as you see, when didSelectClose is called, we dismiss the popup and pop the navigation stack to go to ViewControllerA

Lastly, before you present PopupVC, you must set ViewControllerB as the delegate

func showPopup() {
    let popupVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "popupID") as! PopupViewController

    popupVC.delegate = self
    present(popupVC, animated: true, completion: nil)
}
mohak
  • 603
  • 1
  • 5
  • 11