How to return to Root View Controller - ios

I am currently working on an app which presents a screen modally and another custom progress indicator modally. Is it possible to return to the root View Controller seamlessly?
Home -> screen1-> screen2(Custom progressIndicator)
I want to dismiss the custom progressIndicator (and the screen presented modally) and return to my home (root) View Controller in one go.
self.navigationController?.popToRootViewControllerAnimated(true)
Thank you for the help!

You need to dismiss presented model then you can pop all the pushed view controllers. As presented model would not be in the stack of the navigation.
self.dismissViewControllerAnimated(true, completion: {});
Then you can pop to base view controller.
self.navigationController.popViewControllerAnimated(true);

If you use presentViewController, you can use
[self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
to go back to root view. It works on IOS9.

Swift 4
self.dismiss(animated: true, completion: {});
self.navigationController?.popViewController(animated: true);

In Swift 3 this will work:
self.dismiss(animated: true, completion: {});
self.navigationController?.popViewController(animated: true);

Related

Navigation present to another navigation, how to get back to the root navigation

My root navigation has a StoreDetailsController,and I am in the StoreDetailsController present to the ShoppingBagNavigationController.They can be infinite present. What should I do when I want to go back to the root navigation?
- (void)goBagVC{
UINavigationController *shoppingBag = [[UIStoryboard storyboardWithName:#"ShoppingBag" bundle:nil] instantiateViewControllerWithIdentifier:#"ShoppingBagNavigationController"];
ShoppingBagViewController *myshopVC = shoppingBag.viewControllers[0];
[myshopVC passDetailProduct:self.productDetailModel.productID];
[self presentViewController:shoppingBag animated:YES completion:nil];}
- (void)goRootVC{
[self.navigationController.tabBarController setSelectedIndex:0];
[self.navigationController popToRootViewControllerAnimated:YES];}
I want to go root Controller in goRootVC function. Thanks
As ShoppingBagViewController would be your topmost view after you present it, adding a call dismiss(animated: true, completion: nil) to a button press in ShoppingBagViewController would dismiss that screen.
The StoreDetailsController should be visible once the other screen has been dismissed.
It looks to me like you're confusing modal presentation and pushing onto a navigation stack. Your goBagVC method is modally presenting a navigation controller on top of the current view controller. If you want to push view controllers onto a navigation stack then you need to start with a navigation controller and call pushViewController:animated:
You have to search that view controller in yout navigation controller and then pop to that controller.
Swift
let vc = self.navigationController!.viewControllers.filter { $0 is StoreDetailsController }.first!
self.navigationController!.popToViewController(vc, animated: true)

Dismissing presented view controller won't work

I've got a kind of complicated modal segue setup in my project. I'm trying to dismiss a view controller another view controller previously presented. I'm doing so with this code:
if(self.presentedViewController != nil){
print(self.presentedViewController!)
self.presentedViewController!.dismiss(animated: false)
print(self.presentedViewController!)
}
The prints are there for debugging purposes. They show that the presentedViewController doesn't actually get closed.
Even though I've set animated to false, I still see an animation occuring in the app when dismiss is called. Yet, the VC doesn't actually get dismissed.
Anyone knows a solution?
Apple
The presenting view controller is responsible for dismissing the view
controller it presented. If you call this method on the presented view
controller itself, UIKit asks the presenting view controller to handle
the dismissal.
dismiss(animated:completion:) dismisses the view controller that was
presented modally by the view controller.
https://developer.apple.com/documentation/uikit/uiviewcontroller/1621505-dismiss
If you present a view controller from the app's root, for example:
Presenting view controller
let root = UIApplication.shared.keyWindow!.rootViewController!
root.present(someViewController, animated: true, completion: nil)
You would dismiss it from the presented view controller like so:
Presented view controller
let root = UIApplication.shared.keyWindow?.rootViewController
root?.dismiss(animated: true, completion: nil)

How to present a modal controller from another modally presented controller in iOS

How to present a modal controller from another modally presented controller in iOS?
first, present a controller named sourceSelectorController modally:
self.present(sourceSelectorController, animated: true, completion: nil)
now ,how to present a new controller from sourceSelectorController modally?

Transparent ViewController work with presentViewController, but not pushViewController

let dialog: FriendCheckInDialogViewController = storyboard?.instantiateViewControllerWithIdentifier("FriendCheckInDialogViewController") as! FriendCheckInDialogViewController
dialog.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
If I use
self.navigationController?.presentViewController(dialog, animated: false, completion: nil)
Then transparency works. But popToRootViewControllerAnimated does not, so I cannot go back.
If I use
self.navigationController?.pushViewController(dialog, animated: false)
Then background becomes black instead of transparent. But popToRootViewControllerAnimated works, so I can go back.
What should I do?
You seem to be mixing different presentation styles.
If you present a view controller modally using presentViewController, then you can dismiss it using dismissViewController. In this case you can present the view controller modally over the current view controller.
If you push a view controller onto the navigation controller stack, then you can pop that view controller back off, but the pushed view controller replaces the current view controller, so you can't expose it "underneath".

How to dismass modal viewcontroller without navigationcontroller?

I have two UIViewControllers. There is no embedded navigationcontroller. They are connected by a segue, which is a "Present Modally".
I have a button on scene2. When it is clicked, I call this code:
self.navigationController?.popViewControllerAnimated(true)
I can see the code is hit but nothing happens. What am I doing wrong?
What you are doing wrong is that you are popping a view controller from a non existing navigation controller.
Basically when you say self.navigationController? that returns nil because there is no navigation controller. So the pop function doesn't get called.
What you have to do is call self.dismissViewControllerAnimated(true, completion: nil).
I think that you have to dismiss your controller and not pop it to back action:
self.dismissViewControllerAnimated(true, completion: {});

Resources