How can I dismiss an overCurrentContext modal viewController and its children? - ios

I have the following structure:
From mainVC I present a modal viewController, modalVC:
let modalVC: ModalVC = ModalVC()
modalVC.modalPresentationStyle = .overCurrentContext
modalVC.modalTransitionStyle = .coverVertical
self.present(modalVC, animated: false, completion: nil)
From modalVC I present another viewController chatVC, but this time without the .overCurrentContext option:
let chatVC: ChatVC = ChatVC()
self.present(chatVC, animated: false, completion: nil)
At this point, I have the following:
mainVC --present modally --> modalVC --present--> chatVC
I want to dismiss chatVC and modalVC to show mainVC again.
I tried:
self.presentingViewController?.presentingViewController?.dismiss(animated: false, completion: nil)
No succeed.
Any ideas? Thanks in advance

It can sometimes help to be a little verbose... helps to understand what's going on vs. what you expect to be going on. Try it like this:
if let myPresenter = self.presentingViewController {
if let hisPresenter = myPresenter.presentingViewController {
hisPresenter.dismiss(animated: false, completion: nil)
}
}
If that doesn't "work" you can at least step through in debug to figure out what's not quite right.

Related

Dismissing Modal Presented ViewController Animation is not working

I am simply trying to present a ViewController and dismiss it:
Present:
let completedWishesVC = WishlistViewController()
self.present(completedWishesVC, animated: true, completion: nil)
Dismiss:
self.dismiss(animated: true, completion: nil)
However it ends up looking like this:
Screen-Video
What am I missing here? Let me know if you need any more info!

Application stuck while presenting new VC in swift

I'm trying to present a new VC, but when I hit the code application get stuck and I can't do anything. It's quite a weird issue, how can I get out of it? My code for presenting new VC is this,
let vc : PropertyDetailController! =
UIStoryboard.viewController(identifier: "PropertyDetailController", storyBoard: "Explore") as? PropertyDetailController
vc.propertyDetailData = property
vc.hidesBottomBarWhenPushed = true
vc.modalTransitionStyle = .crossDissolve
vc.modalPresentationStyle = .fullScreen
self.navigationController?.present(vc, animated: true, completion: nil)
This is what it shows in console,
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
It isn't present new VC. This is how it my apps stuck please check gif link,
enter link description here
As I can see in your code you are moving to the new storyboard so it's necessary to move it on through the UINavigationController
Blow one is the sample code that might help you
let storyBoard: UIStoryboard = UIStoryboard(name: "YOUR_STORYBOARD", bundle: nil)
let aVC: YOUR_VC = storyBoard.instantiateViewController(withIdentifier: "YOUR_VC_ID") as! YOUR_VC
let navigationController = UINavigationController(rootViewController: aVC)
self.present(navigationController, animated: true, completion: nil)
if you want to present a view controller, try
self.present(vc, animated: true, completion: nil)
if you want to push a view controller, try
self.navigationController?.pushViewController(vc, animated: true)
Try moving the push code to main thread,
DispatchQueue.main.async {[weak self] in
self?.navigationController?.pushViewController(vc, animated: true)
}
Either present or push, all the UI handling must be done on the main thread.
Edit:
If you want to present vc, use
DispatchQueue.main.async {[weak self] in
self?.present(vc, animated: true, completion: nil)
}
First make sure current view controller embedded with Navigation Controller.
Now use below code to fix this issue.
let storyboard = UIStoryboard(name: "Explore", bundle: nil)
guard let vc = storyboard.instantiateViewController(withIdentifier: "PropertyDetailController") as? PropertyDetailController else {return}
vc.hidesBottomBarWhenPushed = true
vc.modalTransitionStyle = .crossDissolve
vc.modalPresentationStyle = .fullScreen
self.navigationController?.present(vc, animated: true, completion: nil)

Cannot convert value of type 'testViewController.Type' to expected argument type 'UIViewController'

I receive this error with the following code. I have a View Controller class called "testViewController". I'm trying to press a button and make it appear, but this error is showing in my code. Help!
self.present(testViewController, animated: true, completion: nil)
I expect this should show the new view controller but it does not.
You need to pass an instance not
let vc1 = TestViewController.self // wrong
let vc2 = TestViewController() // right
self.present(vc2, animated: true, completion: nil) // right
self.present(TestViewController, animated: true, completion: nil) // wrong
self.present(vc1, animated: true, completion: nil) // wrong
Edit: If the vc is inside storyboard load it like
let vc = self.storyboard!.instantiateViewController(withIdentifier: "VCID") as! TestViewController
self.present(vc1, animated: true, completion: nil) // right
I just found code that works. Thanks all. Here it is:
let testViewController = self.storyboard?.instantiateViewController(withIdentifier: "MyTest") as! testViewController // I created MyTest in the identifier field of the identity inspector.
self.present(testViewController, animated: true)

How can I switch view controllers with code in iOS?

let SecondViewController =
self.storyboard?.instantiateViewController(withIdentifier:
"SecondViewController") as! SecondViewController
self.navigationController?.pushViewController(SecondViewController,
animated: true)
For some reason this doesn't work. Any ideas?
Replace
self.navigationController?.pushViewController(SecondViewController,
animated: true)
with
self.navigationController?.present(SecondViewController, animated: true, completion: nil)
Edit: To avoid the optionals you should also do one of the following:
guard let navController = self.navigationController else { return }
navController.present(SecondViewController, animated: true, completion: nil)
or
if let nacVontroller = self.navigationController {
navController.present(SecondViewController, animated: true, completion: nil)
}
Edit2: You should also avoid force unwrapping your SecondViewController using one of the above methods as well. Although that is not your current issue.

UIModalTransitionStyle.FlipHorizontal doesn't flip

I am new in Cocoa and Swift, and I'm trying to use the UIModalTransitionStyle.FlipHorizontal transition to switch from ViewController1 to ViewContoller2 and vice-verse.
Unfortunately the switch from controller 1 to controller 2 occurs without any transition, but works correctly on dismiss.
I use this code in ViewController1:
let ctrl = ViewController2(nibName: nil, bundle: nil)
ctrl.modalTransitionStyle = UIModalTransitionStyle.FlipHorizontal
self.presentViewController(ctrl, animated: true, completion: nil)
and this code in ViewControler2:
self.presentingViewController.dismissViewControllerAnimated(true, completion: nil)

Resources