Im trying to move one view controller to another without animation:
viewController.dismiss(animated: false, completion: nil)
// viewController.presentingViewController != nil here
tabBarController.present(viewController, animated: false, completion: nil)
When i run this code i have a crash: Application tried to present modally an active controller
You can put the present part in the completion handler of the dismiss call
viewController.dismiss(animated: false) {
tabBarController.present(viewController, animated: false, completion: nil)
}
Related
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!
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)
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.
I am developing an iOS application using Objective-C and I want to use a Swift class (Alert box). I implement the whole function. Without any error I can pass the values. But I am getting the following error message:
"whose view is not in the window hierarchy!"
I've tried different ways:
Method 1
presentViewController(alertController, animated: true, completion: nil)
Method 2
self.presentViewController(alertController, animated: true, completion: nil)
Method 3
var mainView:MainViewController = MainViewController()
mainView.presentViewController(alertController, animated: true, completion: nil)
All methods are resulting the same error message.
In my self I found the answer for this question.
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController,animated: true, completion: nil)
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)