Getting current visible modalViewController from appdelegate - ios

I have a navigation controller which acts as an rootViewController and I have one view controller which is presented modally (not pushed). So my control reaches app delegate while this presented viewController is the one, which is visible. So my question is, how can I get this viewController in appDel? And, No, its not my rootViewController and is not present in navigation stack.

I believe yo may use presentedViewController property of rootViewController. Check this answer for more details: https://stackoverflow.com/a/12684721/1301013

Related

View Controller presented modaly but the segue is a Show Segue

My app start with a MainViewController that has an UINavigationController. If a show segue to another controller, if the new controller has a UINavigationController it seems to be presented modally, but if I remove the UINavigationController it works fine.
Is this normal iOS behavior?
how can I navigate in the controller if there isn't a UINavigationController anymore?
Every time you segue from your MainViewController (that has a navigationController) to a new ViewController, the new ViewController gets added to the MainViewController's NavigationController's stack.
If you segue to a new NavigationController from your MainViewController you create a new navigation stack and therefore iOS displays it like a modal.
To answer your question, you only need the 1 navigation controller on your MainViewController and all subsequent segues will be pushed onto the stack of that navigation controller. To get navigate back through the navigation stack to the MainViewController, have a look at navController.popViewController(animated: true).

Initial View Controller from everywhere

I got an application with a navigation view controller as Initial View Controller. After loading the initial view controller, I set up a notification listener. The Notification can be posted everywhere in the app. I have some pushed vc's and also modally presented. My goal is to return to the initial vc and present a modal view controller from there if the notification is triggered but I have no Idea how to do that. Do I need to do this outside of the MainViewController?
Answer assumes rootViewController is UINavigationController as specified by OP in his question
You can achieve what you want using
(UIApplication.shared.keyWindow?.rootViewController as! UINavigationController).dismiss(animated: true) {
(UIApplication.shared.keyWindow?.rootViewController as! UINavigationController).popToRootViewController(animated: true)
}
Whats happening is pretty simple. Knowing that your initial viewCOntroller is always UINavigationController, initially check if you have anything presented on rootView controller, if yes dismiss it and in the completion block pop to rootViewController of your initial viewController.
Hope it helps

isBeingPresented value is inconsistent

I present a NavigationController with a ViewController in it modally.
In the ViewController I can see that self.navigationController.isBeingPresented is true.
But if I now push a new ViewController on the modally presented NavigationController and pop back to the original ViewController the same call to check isBeingPresented returns false.
Documentation is sparse but I can't really explain this inconsistency other than that it may be a bug?
That's the intended behavior.
isBeingPresented is true only when the given viewController is currently being presented (docs):
A Boolean value indicating whether the view controller is being presented.
and not when it is already presented. It is set to true during the presentation process - from the point when navigation to that view controller starts until the moment when the view controller is fully presented, and all the lifecycle events happened (presentation animations finished, viewWillAppear/viewDidAppear callbacks were called, etc.). After that moment, the view controller is presented, but not is being presented, thus the isBeingPresented will not be set to true anymore.
The self.navigationController was presented at first (by modal presentation), popping a view controller from it does not trigger a presentation. After presenting a UINavigationController, it is presented whole time during pushing and popping view controllers on it. You would have to dismiss the navigationController, and then present it again for the isBeingPresented to be true - because only during modal presentation it is being presented.

SplitViewController primaryViewController has no way of being dismissed

I have a UISplitViewController with default behaviour. When showViewController(..) executes, it presents a new UINavigationController that I have passed. Sometimes this UINavigationController is presented modally (based on TraitCollection).
I have noticed that a UINavigationController (or any VC for that matter) is only presented modally when using a horizontally/vertically compact size class. What is missing however is a method of going back or simply dismissing the UINavigationController. (A back button is only visible if the device goes from compact-regular to regular-compact, in which case the UISplitViewController adds a back button to my presented UINavigationController.)
How do I handle this behavior to be as consistent as possible for dismissing this presented UINavigationController? Should I consider not using a UINavigationController for any particular reason? (and embed a ViewController only in certain cases instead?)
Any help appreciated.

Going from NavigationController to TabBarController

So... I've got a ViewController that's being pushed onto a NavigationController. In interface builder I create a separate ViewController and Embed it into a TabBarController and it looks good in Interface Builder.
In my app, I'm trying to go from one of the ViewControllers in my NavigationView to the ViewController in the TabBarController. How would I do this the correct way? I can't just push the view onto the NavigationController, because the tab bar at the bottom won't show up.
Any help would be greatly appreciate.
I believe you're operating with the UINavigationController and UITabBarController in a backwards order to recommended best-practice.
Unless something has changed in the last year or two (which may have happened) the UINavigationController should never have a UITabBarController pushed onto it. If you are using a UITabBarController in your app, it should be the window.rootViewController, and the navigation controller being member of the UITabBarController's viewControllers array.
I'm trying to go from one of the ViewControllers in my NavigationView
to the ViewController in the TabBarController. How would I do this the
correct way?
In that structure, you'd assign your destination view controller as another element of the viewControllers array. Then, in my style, I'd send a NSNotification something like "LaunchOtherViewController" from your first view controller, and thus you have no need for the first view controller to know about the tab bar controller or second view controller. Then have some class that knows about the second view controller receive that notification, and update the selectedIndex of the UITabBarController to that of the second, destination view controller.
Hope that makes sense.
You need to push the TabBarController onto the view. You may need to set the selected view controller of the tab bar, but it's important the tab bar controller be actually pushed onto the navigation stack (or presented modally).

Resources