View controller transition error - ios

I am receiving this error after dismissing a multipeer connectivity window:
"Warning: Attempt to present < AudioWaves.ViewControllerJoin: 0x176cf2d0> on < AudioWaves.ViewController: 0x17594de0> whose view is not in the window hierarchy!"
Here is my code below,
func browserViewControllerDidFinish(
browserViewController: MCBrowserViewController!) {
// Called when the browser view controller is dismissed (ie the Done
// button was tapped)
let ViewControllerjoin:ViewControllerJoin = ViewControllerJoin()
self.presentViewController(ViewControllerjoin, animated: true, completion: nil)
dismissViewControllerAnimated(true, completion: nil)
}
If I put the dismiss before the presenting of the new view controller the browser dismisses however the app simply brings to a blank black screen with no error.

Chain your two calls like this:
dismissViewControllerAnimated(true, completion: {
self.presentViewController(ViewControllerjoin, animated: true, completion: nil)
})
This way chains first the dismissViewControllerAnimated and then the presentViewController, this prevent your hierarchy from strange behavior
This helped the asker to verify that his ViewController indeed is the black screen: Try changing the background color of the ViewControllerJoin to blue for example and trying again, once I saw that the default backgroundColor caused a black screen in a ViewController (this seems to help the

Related

Swift : present a ViewController while alert is shown

I have a problem with present UIViewController Modaly by above code
self.presentViewController(view, animated: true, completion: nil);
it doesn't work when another view present modaly such as UIAlert,its triggered by a socket packet in background and user may is performing another work and may another modal view already presented when the trigger happens.
You can not present two view controllers at the same time from the same source controller. Instead, try presenting the second view controller from the first one that was presented.
if let presented = self.presentedViewController {
presented.present(vcToPresent, animated: true, completion: nil)
}
else {
self.present(vcToPresent, animated: true, completion: nil)
}

How to correctly close modal presented controller

I am opening a navigationController using either directly navigation segue in storyboard or called segue from code, but still created on storyboard.
Example:
self.performSegue(withIdentifier: "addNew", sender: nil)
Which then with modal transition opens a navigationController. NC then has a few view controllers on which all have close button on them and when I click on it it closes NC using:
self.navigationController?.dismiss(animated: true, completion: nil)
But afterwards when I switch between different ViewControllers on TabbarController or open some other view with push view I get:
Unbalanced calls to begin/end appearance transitions for
And also viewWillAppear on opened view isn't called. So can anyone tell me if I'm closing the NC correctly or is there some other way to prevent the error.
EDIT1:
changed the closing call from
controller.navigationController?.dismiss(animated: true, completion: nil)
to self.navigationController?.dismiss(animated: true, completion: nil) just to make the question easier.
Also tired:
self.dismiss(animated: true, completion:nil)
EDIT2:
Is it maybe bad to open a NC from VC that is on NC that is on UITabbarController or is just simply already bad to have NavigationControllers as tabs on TabbarController?
You may need to close the controller from that controller's side:
// place this to the target controller, which handles you that modal view
self.dismiss(animated: true) {
//completion:
}
or
self.dismiss(animated: true, completion:nil)

View over view breaks hierarchical

I'm using MZFormSheetPresentationController to show as "pop-up" a ViewController2 (embedded in a Navigation controller as suggested) over a ViewController1.
My ViewController1 has a searchbar, a UISegmentedControl and a tableview:
When user clicks on the searchbar bookmark button, pop-up is shown.
I'd like to close the pop-up when user clicks on the done button and it works great using self.dismissViewControllerAnimated(true, completion: nil) method but I'm looking for more. I'd like to present again ViewController1 so tableView will reload data.
I tried with:
self.dismissViewControllerAnimated(true, completion: nil)
print("Dismissed")
//goToTickets
let next = self.storyboard?.instantiateViewControllerWithIdentifier("myTabBar") as! UITabBarController
self.presentViewController(next, animated: true, completion: nil)
but I get this error:
Warning: Attempt to present on whose view is not in the window hierarchy!
Pop-up disappears but I can't present ViewController.
How can I do?
Thanks in advance.
Edit
This is my ViewController2 with identifier "navigationFilter"
and my tabBar:
When you are in the middle of dismissing you are trying to present next ViewController, you have to wait for completion handler and then present next view controller like this:
self.dismissViewControllerAnimated(true, completion: {
let next = self.storyboard?.instantiateViewControllerWithIdentifier("myTabBar") as! UITabBarController
self.presentViewController(next, animated: true, completion: nil)
})

Go back to previous view controller doesn't work

My first view controller has a button, which triggers the #IBAction goTo2ndVc() which presents a second ViewController:
class FirstVC: UIViewController {
...
#IBAction func goTo2ndVc() {
let secondVc = SecondVC(label: "I am second vc.")
self.presentViewController(secondVc, animated: true, completion: nil)
}
When the button is pressed, the 2nd view controller is shown on screen. No problem.
In 2nd view controller, there is also a button which is used to go back to 1st view controller:
class SecondVC: UIViewController {
...
#IBAction func backToFirst(sender: AnyObject) {
print("go back ...")
self.navigationController?.popViewControllerAnimated(true)
}
}
I looked on internet, people suggest to use navigationController?.popViewControllerAnimated(true) to go back to previous controller. But when I press the go back button I can see the print message "go back ..." but the app doesn't go back to 1st view controller. WHY?
#IBAction func backToFirst(sender: AnyObject) {
print("go back ...")
self.dismissViewControllerAnimated(true, completion: nil)
}
In Swift 3
self.dismiss(animated: true, completion: nil)
you should not use navigation controller, because you didn't use it when you were adding the second view controller. that's why simply call dismissViewControllerAnimated method.
You have to use UINavigationController and its pop methods only when you add your view controllers via pushViewController method.
Familiarize yourself with the concept of navigation controller here: https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html
there
the issue is very simple..
self.presentViewController(secondVc, animated: true, completion: nil)
the code will present second view, you are not pushing it.
self.navigationController?.popViewControllerAnimated(true)
the popViewController will pop back to the previous view controller from where it is been pushed.
So, there are two ways you can achieve what you want
1)If you want to present viewController then you have to dismiss the view controller to show previous view controller with
self.dismissViewControllerAnimated(true, completion: nil)
2)If you want to use PopToVewcontroller, then you have to push you second view controller instead of presenting it with
self.navigatioVonroller?.pushViewController(secondVc, animated: true)
If you want to return to the previous view controller, you can simply add:
[self dismissViewControllerAnimated:YES completion:nil];
to the button action method.
If this is added on the nav view controller present on every screen, I see no reason why it shouldn't work as it would always dismiss the most recently presented view.

How to "De-initialize" ViewController

In the first screenshot, LatestPost is set as Is Initial View Controller. When I press Camera tab, it launches the .Camera for UIImagePickerController. However when I hit "Cancel", it dismisses the current ViewController, and my tab bar goes missing as shown on the second screenshot.
The second screenshot is embedded inside a UINavigationController.
I quite certain that has got to do with dismissing my ViewController. The question here is how do I "de-initialized" my ViewController?
My codes as follow:
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
EDIT: Added Storyboard screenshot as requested.

Resources