iOS Can't dismiss view controller
To dismiss and pop to main view you can use alert button action handler.
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: { (action) in
self.dismiss(animated: true, completion: {
self.navigationController?.popToRootViewController(animated: true)
})
}))
Or you can use the navigation to specific view controller using below lines.
for viewController in self.navigationController!.viewControllers {
if viewController.isKind(of: <Your_Main_ViewController_Class_Name>.self) {
self.navigationController?.popToViewController(viewController, animated: true)
break
}
}
Your_Main_ViewController_Class_Name
is the view controller that within your navigation controller stack to which you need to navigate. (ie) main view
To blindly navigate to main view once alert popup displayed, you can use completion handler while present the alert.
self.present(alert, animated: true, completion: {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
self.navigationController?.popToRootViewController(animated: true)
}
})
ios swift cannot dismiss view controller
You are presenting your ViewController
in a wrong way. The following line tells the application to no matter what ViewControllers
you have opened, just throw it out and assign the UINavigationController
what you are creating, as the rootViewController. So the reason your code, the dismiss
, is not working because there are no other ViewControllers
behind your current one.
appDelegate.window?.rootViewController = navigationController // Wrong
This usually goes into the beginning of the applications, where you define with which UIViewController
you want to start your application. I believe you are calling the first code chunk you posted from a UIViewController
. So what you need to do instead of creating a new UINavigationController
and assigning it to the rootViewController
, you just use the UIViewController
s navigation controller to push the next UIViewController
.
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let view_controller1 = storyBoard.instantiateViewController(withIdentifier: "incident_view_id") as! IncidentViewController
navigationController?.pushViewController(view_controller1, animated: true)
Dismiss View Controller Doesn't Working in Helper Class Swift
After two days finding the right way to dismiss my controller and couldn't find any because I think Xcode find that my current controller is nil. Instead, I use this:
let viewController = UIStoryboard(name: "DashboardPreLogin", bundle: Bundle.main).instantiateViewController(withIdentifier: "TabBarPreLoginViewController")
let appDel: AppDelegate = UIApplication.shared.delegate as! AppDelegate
appDel.window?.rootViewController = nil
appDel.window?.rootViewController = viewController
UIView.transition(with: appDel.window!, duration: 0.5, options: UIViewAnimationOptions.transitionCrossDissolve, animations: {() -> Void in appDel.window?.rootViewController = viewController}, completion: nil)
This will dismissing view controller and replace with new controller.
How to correctly dismiss previous view controller
Think of it this way: A view controller can't exist on an island. It has to be presented on top of something.
That means when you present one VC on top of another, the presenting view controller is the "foundation" for the new one you just presented.
If you don't want to present VCs on top of each other, you have a couple of options:
1) Use a navigation controller. This is probably the best approach. You can present or push any view controller. If you decide to push, you can remove the old one from the navigation stack, or you can keep it there so the user can go back. There are lots of ways to use a navigation controller, and it's easily the most flexible way to navigate between controllers.
2) Use a tab bar controller. This works best if you have just a few different view controllers in your app, but it's good for certain use cases.
3) Do exactly what you said in your post (use the root view controller to present/dismiss all other VCs). As I said, you can't present a view controller out of thin air-- there always has to be something behind it. Unless there's a ton of stuff going on in your root VC, this shouldn't cause any memory issues. This approach should be fine unless you're very particular about the animations between your view controllers.
In general, I wouldn't worry too much about memory usage until it becomes a problem. It should be fine to present view controllers on top of each other for 99% of normal use cases.
Related Topics
How to Remove Single Object in Array from Multiple Matching Object
Adding Firebase Data to an Array in iOS Swift
Dynamically Passing Closure with Keypaths to a Sorting Function
How to Authorize Twitter with Swifter
Array Element Checking in Swift
Get "No Keychain Available" Error When Try to Access Keychain from App Extension
Saving an Image to Photos Library Using Swift 2.0
How to Deal with Dynamic Sections and Rows in iOS Uitableview
How to Use Tap Gesture in Accessibility in Swift
Give Thumbnail Image with Uiactivityviewcontroller
Create Hash in Swift Using Key and Message
Creating a Rtsp Client for Live Audio and Video Broadcasting in Objective C
Viewing Os_Log Messages in Device Console
Format String with Variadic Arguments
How to Get the Opposite Value of a Bool in Swift
Swiftui Animation and Subsequent Reverse Animation to Original State
Iad Interstitials Not Showing Consistently? and Not at All on the Simulator