Can't Dismiss Navigation Controller in Swift

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 UIViewControllers 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



Leave a reply



Submit