Reputation: 2326
Let's say I have 3 ViewControllers
labeled "A","B" and "C". Right now, "A" is the rootViewController
of the window and it presents "B" modally. When a button is tapped in "B" it is supposed to present C modally immediately and dismiss "B" automatically. How can I do that? I saw all the examples in StackOverflow but they are not working
@IBAction func proceedBtnTapped(_ sender: Any) {
weak var pvc = self.presentingViewController
let vc = ThirdViewController()
pvc?.present(vc, animated: true, completion: { [weak self] in
self?.dismiss(animated: true, completion: nil)
})
}
This is my code. Any help?
Upvotes: 3
Views: 3214
Reputation: 4956
Using delegation pattern EX:UIImagePickerViewControll
class A:UIViewController, BDelegate {
@IBAction func onOpenBController(_ sender:Any) {
let bVC = B()
bVC.delegate = self
present(bVC, animated: true, completion: nil)
}
func bControllerDidSelect(_ controller: B) {
controller.dismiss(animated: true, completion: nil) //Dismiss B controller
present(C(), animated: true, completion: nil) //Present C controller
}
}
class B:UIViewController {
weak var delegate:BDelegate?
@IBAction func onSomeClickEvent(_ sender:Any) {
delegate?.bControllerDidSelect(self)
}
}
protocol BDelegate: class {
func bControllerDidSelect(_ controller:B)
}
class C:UIViewController {
}
Upvotes: 2
Reputation: 2326
Thanks for the help, finally I have done this using delegates and protocols. We need to create the protocol in SecondViewController and we have to use that protocol in FirstViewController.
// Declare protocol and variable delegate in SecondViewController
protocol dismissVC {
func presentVC()
}
var delegateVC: dismissVC? = nil
// Call that protocol in FirstViewController
class FirstViewController: UIViewController, dismissVC {
func presentVC() {
let pickVc = UIStoryboard(name:"Main", bundle:
nil).instantiateViewController(withIdentifier: "third-VC")
as! ThirdViewController
pickVc.modalPresentationStyle = .custom
present(pickVc, animated: true, completion: nil)
}
}
// Action method in SecondViewController
@IBAction func proceedBtnTapped(_ sender: Any) {
self.dismiss(animated: true) {
self.delegateVC!.presentVC()
}
// and at last call that delegate when you try to use segue
let SecondVc = UIStoryboard(name:"Main", bundle: nil).instantiateViewController(withIdentifier: "second-VC") as! SecondViewController
Vc.modalPresentationStyle = .custom
Vc.delegateVC = self // add delegate here
self.present(Vc, animated: true, completion: nil)
})
// At last call delegate
yourVc.delegateVC = self
Upvotes: 3
Reputation: 4514
if you use segue
then use 'Unwind segue
'
for this write given code in your 'A view controller'
@IBAction func unwindToThisViewController(segue: UIStoryboardSegue) {
self.performSegue(withIdentifier: "YourSegueForC_VeiwController", sender: nil)
}
In B_viewcontroller, On press button call 'Unwind segue'. Then firest B_VC dismiss and then C_VC present on A_VC.
Note:- only one View_controler present on any View_controler. If you want to add another first remove or dismiss another Viewcontroller from that View controller.
Upvotes: 3
Reputation: 665
First dismiss your b view controller and then present c view controller on A.
try this.
let vc = ThirdViewController()
let navVC = UINavigationController(rootViewController: vc)
if let parent = self.presentingViewController{
self.dismiss(animated: true){
parent.present(navVC, animated: true)
}
}
Upvotes: 3