Transition without loose data with Storyboard and Swift - ios

I'm developing a new project in Swift but i've a big (and dumb) problem.
I've three viewControllers:
1.- Call webservices, take JSON and pass this JSON to the second view
2.- Parsing data and put the data in the view (tableview and everything ok). At this view I go to other view with a button (to the view 3). I don't need pass nothing.
3.- I try to go back to view 2 but the view is empty because the data who posicioning at the tableview was pass from view 1.
I try with a lot of solutions:
navigationController!.dismissViewControllerAnimated(false, completion: nil) -> If i link the storyboard with a transition from view 3 to view 2, the view 2 is empty, if i don't link in storyboard -> unexpectedly found nil while unwrapping an Optional value
If i change "!" for "?" do nothing
The same with every code i have been test.
My code is:
View 1 to view 2: (connected in the visual part of Storyboard (IB))
#IBAction func goRecibosPendientes(sender: UIButton) {
let controllerVC = self.storyboard!.instantiateViewControllerWithIdentifier("recibos") as! TablaRecibosTableViewController
controllerVC.initWithJSON(self.jsonReceived)
self.presentViewController(controllerVC, animated: true, completion: nil)
}
View 2 to View 3: (connected in the visual part of Storyboard (IB))
#IBAction func goToTablaProveedoresDeServicios(sender: UIButton) {
let controllerVC = self.storyboard!.instantiateViewControllerWithIdentifier("proveedoresDeServicios") as! TablaProveedoresDeServicios
self.presentViewController(controllerVC, animated: true, completion: nil)
}
View 3 to 2: I try every kinds of do that (every kinds i know, not EVERY kinds)
#IBAction func backPressed(sender: UIButton) {
navigationController!.dismissViewControllerAnimated(false, completion: nil)
navigationController!.dismissViewControllerAnimated(true, completion: nil)
navigationController!.popToRootViewControllerAnimated(true)
}
Thanks a lot.

If you are using NavigationController than try to push a view controller it will remains your previous view in stack.
self.navigationController.pushViewController(controllerVC, animated: true)

Related

If I present a ViewController programmatically without using a Navigation Controller, does the new VC "replace" the old one, or does it stack on top?

I'm new to iOS.
I have an app where the path through the app can vary depending on the configuration I fetch from an API. Because of this, I don't use segues because I would need to create a segue from each ViewController (VC) to EVERY other VC. It creates a mess of segues that I don't want. So Instead I navigate from screen to screen like this:
func navigate(to viewController: String) {
let storyboard = UIStoryboard(name: K.mainStoryBoard, bundle: nil)
let nextVC = storyboard.instantiateViewController(identifier: viewController)
self.present(nextVC, animated: true, completion: nil)
}
My question is this: If I would have embedded my VCs in a NavigationController I know it would have created a stack. When I get to the last screen I would call func popToRootViewController(animated: Bool) -> [UIViewController]? and start from the beginning. However, I don't use a NavigationController. So, does that mean that when I present the next VC it replaces the previous one or does it stack on top of the previous one? I'm trying to prevent memory leaks so I want to make sure I don't keep stacking the VCs on top of each other until my app runs out of memory and crashes.
Thanks in advance
Edit
So, in my final VC I created an unwind segue. And I call it like this: performSegue(withIdentifier: "unwindToMain", sender: self)
and In my first VC (the initial VC in my app) I write this:
#IBAction func unwind( _ seg: UIStoryboardSegue) {
}
Everything works fine the first trip through the app. The last VC unwinds back to the fist VC. The problem is now that when I try to run through the app again (starting from VC 1 and then going to the next one) I now get this error:
MyApp[71199:4203602] [Presentation] Attempt to present <MyApp.DOBViewController: 0x1038760c0> on <MyApp.ThankYouViewController: 0x112560c30> (from <MyApp.ThankYouViewController: 0x112560c30>) whose view is not in the window hierarchy.
To make sense of this, DOBViewController would be the second VC I want to go to from the MainVC. ThankYouViewController is my last VC. It looks as if it isn't completely removed from the stack. Can anyone tell me what's going on?
Here is a very simple, basic example...
The controllers are setup in Storyboard, each with a single button, connected to the corresponding #IBAction.
The DOBViewController has its Storyboard ID set to "dobVC".
The ThankYouViewController has its Storyboard ID set to "tyVC".
MainVC is embedded in a navigation controller (in Storyboard) and the navigation controller is set to Initial View Controller:
class MainVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.setNavigationBarHidden(true, animated: false)
}
#IBAction func pushToDOB(_ sender: Any) {
if let vc = storyboard?.instantiateViewController(withIdentifier: "dobVC") as? DOBViewController {
navigationController?.pushViewController(vc, animated: true)
}
}
}
class DOBViewController: UIViewController {
#IBAction func pushToTY(_ sender: Any) {
if let vc = storyboard?.instantiateViewController(withIdentifier: "tyVC") as? ThankYouViewController {
navigationController?.pushViewController(vc, animated: true)
}
}
}
class ThankYouViewController: UIViewController {
#IBAction func popToRoot(_ sender: Any) {
navigationController?.popToRootViewController(animated: true)
}
}
does that mean that when I present the next VC it replaces the previous one or does it stack on top of the previous one?
The new one stacks on top of the previous one.
When you present a view controller, like
self.present(nextVC, animated: true, completion: nil)
The one you called .present on (self in this case) becomes presentingViewController for the nextVC instance.
The one you presented (nextVC in this case) becomes presentedViewController for the self instance.

Using instance variable to indicate the view is Edit or Show

In Start Developing iOS Apps (Swift): Implement Edit and Delete Behavior
, The offical tutorial tell me should use presentingViewController and navigationController to indicate the specified view is Edit or Show, Like following code:
#IBAction func cancel(_ sender: UIBarButtonItem) {
// Depending on style of presentation (modal or push presentation), this view controller needs to be dismissed in two different ways.
let isPresentingInAddMealMode = presentingViewController is UINavigationController
if isPresentingInAddMealMode {
dismiss(animated: true, completion: nil)
}
else if let owningNavigationController = navigationController{
owningNavigationController.popViewController(animated: true)
}
else {
fatalError("The MealViewController is not inside a navigation controller.")
}
}
The adding view is presented by modal, the editing view is presented by embed navigation controller, But I think this approach is not good to understand and easy maintain, How about introduce a isEditOrShow instance varible in the view to indicate the state? like following:
#IBAction func cancel(_ sender: UIBarButtonItem) {
// Depending on style of presentation (modal or push presentation), this view controller needs to be dismissed in two different ways.
if isEditingOrShow = .edit{
dismiss(animated: true, completion: nil)
}
else isEditingOrShow = .show{
owningNavigationController.popViewController(animated: true)
}
}
The tutorial explains that, there are two ways in which you can dismiss view controllers.
For example, when you present a view controller as modal, you can use the below code to dismiss it.
dismiss(animated: true, completion: nil)
However, if you are using a push presentation (Navigation controller), then you should use the below code to dismiss it.
owningNavigationController.popViewController(animated: true)
How about introduce a bool isEditOrShow instance varible in the view to indicate the state?
From what I understand, you won't be needing isEditOrShow variable. If you have any questions, let me know.

How to dismiss 2 modal view controllers without weird animation [duplicate]

This question already has answers here:
Dismissing multiple modal view controllers at once?
(7 answers)
Closed 4 years ago.
I recreated the question and describe its essence more precisely.
I have two view controllers presented modally on Swift 4, without storyboard (can't use unwind) and without navigation controller
A presents B which presents C.
The reason why we don't use navigation controller, it's because we what simple animation from bottom to top and instead of breaking the standard animation from right to left, we decided to use present.
I would like to dismiss 2 view controllers and go from C to A.
Please, don't mark this question as duplicate before you read my question carefully. I found a tone of similar post, but neither solved my problem. Some of them Objective-C or some of the suggest to use:
self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
Or:
self.presentingViewController?.dismiss(animated: false, completion: nil)
self.presentingViewController?.dismiss(animated: true, completion: nil)
It's works, but it create weird animation. It's just delete C and animate dismiss for B:
Expected result:
Idea: you need to dismiss third controller with animation and after dismissing you need to dismiss second without animation. While third controller is being dismissed, second shouldn't be visible.
First, set Presentation style of second view controller to Over Current Context when you're presenting it (since we will need to hide its view when we will dismiss third controller)
let vc2 = VC2()
vc2.modalPresentationStyle = .overCurrentContext
present(vc2, animated: true)
continue with creating callbacks properties for willDismiss and didDismiss inside third controller. This callback will be called before and after you dismiss third controller
class VC3: UIViewController {
var willDismiss: (() -> Void)?
var didDismiss: (() -> Void)?
#IBAction func dismissButtonPressed(_ sender: UIButton) {
willDismiss?()
dismiss(animated: true) {
self.didDismiss?()
}
}
}
then in second view controller in the place where you present third view controller, set third controller's callback properties: declare what happens when third controller will dismiss: you need to hide view of second and then after dismissing third you need to dismiss second without animation (view can stay hidden since view controller will be deinitialized)
class VC2: UIViewController {
#objc func buttonPressed(_ sender: UIButton) {
var vc3 = VC3()
vc3.willDismiss = {
self.view.isHidden = true
}
vc3.didDismiss = {
self.dismiss(animated: false)
}
present(vc3, animated: true)
}
}
Anyway, second option is using UINavigationController and then just call its method popToViewController(_:animated:).
Create a snapshot from the currently visible view and add it as a subview to the first presented view controller. To find that you can simply "loop through" the presenting view controllers and dismiss from the initial one:
#IBAction func dismissViewControllers(_ sender: UIButton) {
var initialPresentingViewController = self.presentingViewController
while let previousPresentingViewController = initialPresentingViewController?.presentingViewController {
initialPresentingViewController = previousPresentingViewController
}
if let snapshot = view.snapshotView(afterScreenUpdates: true) {
initialPresentingViewController?.presentedViewController?.view.addSubview(snapshot)
}
initialPresentingViewController?.dismiss(animated: true)
}
This is the result with slow animations enabled for the dismissal:
https://www.dropbox.com/s/tjkthftuo9kqhsg/result.mov?dl=0
If you are not using a navigation controller and you want to dismiss all ViewControllers to show the root ViewController (assuming A is the root):
self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
Use this in button action method.
The current VC will dismiss when you dismiss the parent VC.
This will dismiss both VCs in single animation.
self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)

UIPageViewController stacking ViewControllers when segueing to home page

I have two storyboards, a main then a second storyboard which houses a UIPageViewController. Inside the second storyboard, an exit button allows a user to return to the home screen which is on the main storyboard.
When I attempt to segue back to the home screen, the page controller views are stacking. I have tried to use:
self.navigationController?.popViewController
Along with some other methods to deallocate the UIPageViewController sets. Nothing seems to work? How do I fix this?
You can see in the image below where the controllers are marked by (3).
Memory Graph Image
Below are a few of my attempts. I am using a notification to invoke a method on the root controller after the user confirms via a yes/no custom modal.
func attempt1() {
self.viewControllerList.forEach {
index in
index.removeFromParentViewController()
index.dismiss(animated: false, completion: nil)
index.navigationController?.popViewController(animated: false)
}
self.performSegue(withIdentifier: "unwindHome", sender: self)
}
func attempt2() {
self.childViewControllers.forEach {
c in
print("CHILD...>", c)
c.removeFromParentViewController()
c.dismiss(animated: false, completion: nil)
}
}
func attempt3() {
(view.window?.rootViewController as? UIPageViewController)?.dismiss(animated: true, completion: nil)
self.dismiss(animated: false, completion: {
self.viewControllerList.forEach {
i in
i.navigationController?.popViewController(animated: true)
}
})
self.performSegue(withIdentifier: "unwindHome", sender: self)
}
Here were my issues:
On the first storyboard, my nav controller was not set to initial.
After setting it to initial, i then linked from the main SB to the second SB
Delegates var's were not set to weak
RxSwift was not being disposed of properly.
I was at first doing pub/sub via Variables as a Global. When moving the observer into the root of my UIPageViewController, upon page breakdown the observers were disposed of causing the onComplete callback to hit.

Load data with Core Data in UICollectionView before animation

I am pretty new to iOS development, but I could not find an answer to this question (maybe I could not find the right keywords to search..?).
Anyway, I have this "problem":
I have 2 storyboards, A and B, both with some views inside.
I go from view A-2 to view B-1 via #IBAction - a simple button click - with the following code:
#IBAction func onExercisesButtonTapped(_ sender: AnyObject) {
let storyboard = UIStoryboard(name: "Exercises", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "ExercisesVC") as UIViewController
controller.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
present(controller, animated: true, completion: nil)
}
On the other hand, in the B-1 view I have a UICollectionView that is populated with data coming from a CoreData model.
The cells within the UICollectionView appear only after the animation is completed. If I set animated: false in the perform function and thus no animation is performed from A-2 view to B-1 view the cells are immediately visible.
The cells are the only element that appear after the completion of the animation. What is the cause of this behaviour? Can I avoid it with some sort of preloading/prefetching or other methods?
Is there a way to display the cells before the animation starts, just like the UICollectionView background and all the other elements within the view?
Thank you in advance :)
Update your view in viewWillAppear:.

Resources