iOS/ swift : moving between view controllers in swift - ios

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)
}

Related

dismiss all previous viewcontroller

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.

Swift 3 : Switch ViewControllers programmatically

I am creating an application which shows a PageVC: UIPageViewController as intro and guide to app.
After navigating through the intro, a Next Button leads to "SettingsVC" which is used to store default settings for the app.
Settings have to be chosen by the user initially although can be changed later.
PageVC ---> SettingsVC
A Save and a Cancel button on the SettingsVC leads to the MainVC of the app.
A button on MainVC leads to SettingsVC.
SettingsVC <---> MainVC
The app would work as follows:
if isFirstLaunch{
instantiate PageVC
}else{
instantiate MainVC
}
in PageVC
nextButtonPressedinPageVC{
instantiate SettingsVC
}
in SettingsVC
if saveButtonPressed && cameFromPageVC{
instantiate MainVC
}
if cancelButtonPressed && cameFromPageVC {
do Nothing
}
if saveButtonPressed && cameFromMainVC{
dismiss currentVC
}
if cancelButtonPressed && cameFromMainVC {
dismiss currentVC
}
in MainVC
if settingsButtonPressedinMainVC {
instantiate SettingsVC
}
I have made sure that if it is application's first launch, PageVC will be instantiated else MainVC will be instantiated.
How can I move between the viewControllers without a possible memory leak i.e. where to performSegue and where to dismiss current VC?
Please include code for reference.
There are many ways to do this, here is one that I find very straightforward because you can do most of the work in your Storyboard:
Think of your MainVC as the rootViewController and the other two as accessory views that will only temporarily be shown. The MainVC should always be your entry point, so set it as the initial VC in your Storyboard.
The other two should be displayed modally so that you can easily return to the MainVC by dismissing them, no matter how you opened them in the first place.
To do this, draw a segue from your MainVC button to the PageVC and name it "showPageVC". From the Next button in your PageVC, draw another segue to the SettingsVC. Now you need some code to handle the dismiss actions: put this snippet in your MainVC:
#IBAction func unwindToMain(segue:UIStoryboardSegue) {
}
This function is just a marker, so it doesn't need a body. It just enables you to create a unwind segue back to MainVC: For each of the buttons in SettingsVC, hold Ctrl and draw from the button to the right exit icon in the header of the SettingsVC storyboard scene and choose unwindToMain in the tiny black popup.
Now you only have to implement the logic to decide if you want to show the PageVC or not in viewDidAppear() of the MainVC. So the whole code would look something like this:
class MainVC: UIViewController {
var didDisplayPageVC = false
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if (didDisplayPageVC == false) {
performSegue(withIdentifier: "showPageVC", sender: self)
didDisplayPageVC = true
}
}
#IBAction func unwindToMain(segue:UIStoryboardSegue) {
}
}
The rest is in the storyboard. If this little proof-of-concept is working, you can go and configure the segues (you might want to remove the animation) etc.

Removing all previously loaded ViewControllers from memory iOS Swift

I am currently editing a code previously written,
In the application, The first viewController is a table view controller named: ListViewController that has several elements, each selection of row creates a new instance of a view controller and presents it modally. but in those view controllers, instead of dismissing them, the previous developer again created the instance of the ListViewController and presents it modally to go back.
The application is obviously using alot of memory.
Dismissing the view controllers is not an option.
if I pop view controllers in the stack one by one, this doesn't work, each view has popups etc presented on viewdidAppear.
I need to remove all previously loaded ViewControllers from memory and present a viewController such that there are no instances of any ViewControllers left in the memory.
Is it possible?
Is there a way i can goto say a new ViewController called HomeViewController ensuring that all previously loaded instances of all view controllers are released.
The scenario is as following:
ListViewController
/ | \
AViewController BViewController CViewController
ListViewController has 3 elements
A
B
C
user can tap any of them, that results in presenting a ViewController.
and from each of the view controllers, when back button is pressed, The ListViewController is presented.
Views are presented using the following code:
if let listViewController = storyboard!.instantiateViewControllerWithIdentifier("ListViewController") as? ListViewController {
self.presentViewController(listViewController, animated: true, completion: nil)
}
I am not sure it will work. Try this. Before presenting any new ViewController.
Make a method in AppDelegate
func switchControllers(viewControllerToBeDismissed:UIViewController,controllerToBePresented:UIViewController) {
if (viewControllerToBeDismissed.isViewLoaded && (viewControllerToBeDismissed.view.window != nil)) {
// viewControllerToBeDismissed is visible
//First dismiss and then load your new presented controller
viewControllerToBeDismissed.dismiss(animated: false, completion: {
self.window?.rootViewController?.present(controllerToBePresented, animated: true, completion: nil)
})
} else {
}
}
Now lets say you move like this
ViewController --> You click a button and present a SecondViewController
So currently we have ViewController and SecondViewController in memory.
Now when you click some button in SecondViewController in order to present a ThirdViewController, then SecondViewController must dismiss. So in SecondViewController button Press
#IBAction func buttonPress(_ sender: AnyObject) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let controllerToBePresented = self.storyboard?.instantiateViewController(withIdentifier: "ThirdViewController") as! ThirdViewController
appDelegate.switchControllers(viewControllerToBeDismissed: self, controllerToBePresented: controllerToBePresented)
}
So now we have ViewController and ThirdViewController in memory.
SecondViewController is removed from memory.
Better solution is to keep your controllers in UINavigationController stack because you can get an array of all ViewControllers pushed on stack.

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

Swift: miss tab bar after click Back button

I'm new to swift and IOS development. I'm working on a tabbed application(3 views). For example, FirstView, SecondView and ThirdView. FirstView has a button that opens a addNewSession view and the addNewSession view has a Back button that back to the FirstView. The problem is Tab bar disappears after back from the addNewSession view
FirstView to addNewSession view.
#IBAction func toAddNew(sender: AnyObject) {
let addNewSession = self.storyboard?.instantiateViewControllerWithIdentifier("addNewSession") as addNew
self.presentViewController(addNewSession, animated: true, completion: nil)
}
addNewSession view to FirstView
#IBAction func backToPrev(sender: AnyObject) {
println("test1")
let FirstView = self.storyboard?.instantiateViewControllerWithIdentifier("FirstView") as FirstViewController
self.presentViewController(FirstView, animated: true, completion: nil)
}
The problem is your backToPrev method is instantiating a new FirstViewController, which is not the same instance you came from. You are not really going back to the original one, you are showing a new one. This is not what you want.
The proper way to do this is to embed the FirstViewController in a navigation controller, then push the addNew controller onto it. When you use a nav controller, you get the Back behavior for free.
Hopefully you are using a storyboard? Select your FirstViewController, go to the Editor menu and choose Embed in Navigation Controller.
In your toAddNew, instead of presentViewController use self.navigationController.pushViewController to push your addNew controller.
There's an even easier way to do this last step, using segues. You control drag in the storyboard from your button in FirstViewController to the addNew controller and create a Show segue. This will automatically show your addNew controller when the button is touched. With this approach, you will want to remove your toAddNew IBAction and the connection since it's redundant.

Resources