dismiss all previous viewcontroller - ios

It is given that I use :
LoginViewController
MainTabController
I use navigationController to embed my tabbed viewContollers on MainTabController.
I call self.dismiss to cancel my viewController, back to the LoginViewController page
If I add the viewController at the middle as below :
LoginViewController
SelectViewController
MainTabController
How can I dismiss all previous view controllers, back to LoginViewController from my tabbed viewController ?

self.view.window!.rootViewController?.dismiss(animated: true, completion: {
print("All controller dismissed successfully..")
})

Use an unwind segue. Unwind segues will generally figure out how to dismiss VCs for you.
Add an #IBAction method called unwindFromLogin(_ sender: UIStoryboardSegue). This method will be called when the unwind segue is performed.
In the storyboard, control+drag from login VC to the "Exit" of main tab VC, select the method you just wrote. Now you have added an unwind segue.
Give the segue an identifier and call performSegue(withIdentifier:sender:) with the identifier when you want to dismiss the login VC.

Related

View Controller get stuck on calling unwind segue in Swift

I have a bit complex hierarchy of calling multiple VC's and then at the end unwind to particular VC again, My flow is like this,
I have a VC that opens with these two conditions,
HomeVC -> DetailVC -> VC1 -> An API call to check if data exist, if yes then present ListVC if not then present Form VC
If FormVC is opened then after submitting form i have to show ListVC
If ListVC is opened first then it simply present Form VC, as in UI ListVC comes first and FormVC is the second one.
Now when i comes directly from FormVC to DetailVCthrough unwind segue, detail vc screen get stuck, now action is performed on it neither it scrolls. I'm too much confused why it is showing such behaviour. How i can fix it? why it is stuck my app?
This is how i call Unwind segue in DetailVC,
#IBAction func unwindToPropertyDetail(sender: UIStoryboardSegue)
{
}
and in FormVC, I just performSegue method like this to come on DetailVC,
self.performSegue(withIdentifier: "GoToDetails", sender: self)
Have you try using dispatch queue?

iOS/ swift : moving between view controllers in swift

suppose I have a MainTabBarController, from one of it's tabs, I go to FirstViewController, then from FirstViewController, I go to SecondViewController (all with present modally), in SecondViewController when user hits cancel button, I want to go back to MainTabBarController, without showing FirstViewController, can I do this without NavigationController? cus I have no NavigationController in current version of my code and it will cost me many changes :(
in your SecondViewController you can call below code on tap of cancel button, which gets the presentingViewController of SecondViewController -> presentingViewController of FirstViewController which is a TabBarController and call dismiss on it.
#objc func dismiss(_ button: UIButton) {
self.presentingViewController?.presentingViewController?.dismiss(animated: true)
}

Unwind from OverCurrentContext modal presentation doesn't work

I am having a problem similar to this:
Cannot Get Unwind Segue Working with Modal View Controller Presented Over Current Context
I presented a VC modally using OverCurrentContext presentation style following a tutorial. All set in storyboard.
VC1 -> modally overCurrentContext -> VC2
The problem is that Xcode 8 has slightly different options in the Attributes Inspector, no OverCurrentContext option in the Segue, so I had to select the presentation: default in the Segue
and set the presentation: overCurrentContext in the VC2
Now, when I try to unwind back to the VC1 I can't, nothing happens and I get stuck in VC2.
I had to add some extra code to the unwind method to make this work.
#IBAction func unwindFromAdvancedOptions(segue: UIStoryboardSegue) {
if segue.source as? VC2 != nil {
segue.source.dismiss(animated: true, completion: nil)
}
}
But I'd really like to understand why storyboard settings where enough to unwind any other modal presentation styles but OverCurrentContext.

dismiss two controllers Swift

I have this situation :
I have a first view controller , when tap on button in it I open in modal mode another view controller , in this view controller when I tap another button I open in modal view another view controller and in it there is a button and when I tap on it I want to go to first view controller without re-initialize it.
How do I do it?
This is the perfect situation for an unwind segue.
Put this in your first viewController (the one you want to return to):
#IBAction func backFromVC3(_ segue: UIStoryboardSegue) {
print("We are back in VC1!")
}
Then in the Storyboard in your 3rd viewController, control-drag from your button to the exit icon at the top of the viewController and choose backFromVC3 from the pop-up.
Now, when the user presses the button in VC3, both VC3 and VC2 will be dismissed and you will return to VC1.
If you are not using Storyboards, you can dismiss the viewControllers with code. Here is code for a button's handler to dismiss two levels of viewController:
func doDismiss(_ sender: UIButton) {
// Use presentingViewController twice to go back two levels and call
// dismissViewController to dismiss both viewControllers.
self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
}
Thanks all for reply and edited my question :)
I found 2 line code to resolved my problem:
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window!.rootViewController?.dismissViewControllerAnimated(true, completion: nil).
And that work well.
Thanks very much

Remove view from RAM swift

I'm using segue navigation with self.perfomSegueWithIndentifier function with type of show segues.I noticed after every navigation other view/viewcontroller did not remove from RAM.What I have to do to solve this
This is what you are looking for:
On Button Tap on View Controller 1:
self.navigationController.pushViewController(secondViewController, animated: true)
On Button Tap on View Controller 2:
self.navigationController?.popViewControllerAnimated(true)
Programmatically
Lets say you have VC1 embed in UINavigationController for now VC1 becomes the top view controller on the navigation stack.
So if you are going VC1 -> VC2
self.navigationController?.pushViewController(vc2, animated: true)
In that case VC2 becomes the top view controller on the navigation stack.
and while coming back from VC2 -> VC1 (Button tap on VC2 perform this statement)
self.navigationController?.popViewControllerAnimated(true)
In that case again VC1 becomes the top view controller on the navigation stack.
Using Segue
Just use Show(e.g Push) and set the identifier for that like i have pushView
and invoke
self.performSegueWithIdentifier("pushView", sender: nil) for moving VC1 -> VC2
and for coming back to VC2 -> VC1
use the same self.navigationController?.popViewControllerAnimated(true)

Resources