Issue with increase memory usage when looping UIViewControllers using UINavigationController - ios

I'm trying to loop through several UIViewControllers: where the 1st (start) view is a menu and the final view serves as the results page that loops back to the starting view, after clicking my bar button "Finish".
Testing with the storyboard, I of course use push segue to navigate through the views, but then employ a modal segue with the "Finish" button to bring me back to the starting view controller, which has the embedded navigation controller. This prevents me from using the back button to go past the starting view and undoing past loops. This is a good thing (took me a while to figure out), as I don't want to cycle farther back then the starting view of one loop.
My issue with what I have designed is my memory usage keeps growing, and I do not know how to deallocate/free the views from the previous loops. I plan to use arrays, images, tableViews within this application so I really need some guidance on how to free those large amounts of memory once the "Finish" button is pressed and the application segues to the starting menu view.
I'm using Xcode version 5.1.1. simulating iPhone 3.5-inch.
Thank you

You say:
... but then employ a modal segue with the "Finish" button
You do not want to to a modal segue with the finish button (because that keeps all the old view controllers in memory and instantiates a new copy of the first scene). If you don't dismiss all of those old view controllers, you'll end up "abandoning" the memory associated with those scenes.
Instead you want to use an unwind segue. This returns to a particular view controller, dismisses any intervening scenes (if any), and releases the memory associated with them and their view controllers.
In your first view controller, create a unwind action:
- (IBAction)unwindToFirstScene:(UIStoryboardSegue *)segue {
// this is intentionally blank
}
Having created that in the first view controller (the destination of the "finish" button), create a segue between the "finish" button in that last view controller to the exit outlet:
When you do that, you'll be prompted for the unwind action.

Related

Changed segue animation to "Show" but it's stuck in "Modal"

After creating ViewController informational pages which use UISwipeGesture to swipe left and right between 4 different UIViewControllers, I first selected "Modal" as the segue animation. But modal animation is contrary to the left/right swipes which should have "Show" segues which better match the user action. When changing the segue types in Interface Builder to "Show" the animations did not change. When de-selecting the "Animates" checkbox, XCode WILL eliminate the animation entirely, but then when selecting "Show" it goes right back to a modal segue animation.
Would show a picture of the set-up, but don't know how in StackOverflow
Tried embedding whole stack into a NavigationController and that didn't work.
Tried deleting segue and reforming it as "Show" from the getgo, that didn't work.
Shut down XCode and Simulator and restarted-- that didn't work.
Yet in another part of the same storyboard, "Show" segue works fine.
The problem was my segues were selected with Show Detail (replace) instead of Show (eg Push). When in interface builder you'll see you get presented with both of those as the top two selections. When using a UINavigationController, it expects Show (eg Push) to be selected which will put a Navigation header at the top of your UIViewControllers with a back button. In the case above, I'll simply write a short amount of code to hide the nav-header at the top of all my informational pages. I will keep the UIGestureControl left/right swipe code as a novelty item, but there are other ways to allow the user to swipe left and right to new pages (lookup PageViewControllers).
UINavigationControllers and Show (eg Push) animation go hand in hand, and you have to be wary of Apple UX/UI guidelines which protect the sanity of users before you go getting experimental with animations in segues.
Show (eg Push) arranges previous, current, and future ViewControllers like a stack of pancakes, so when you move from one to another you're pushing the top off like the top card of a deck of cards. Whereas Show Detail (Replace) acts like you're removing the card and replacing it with the next card. Best way to think of the difference between those two segues.
Not sure why compiler kept the modal segue after I changed it to Show Detail (replace)-- could be a bug with UINavigationControllers?

I need some clarity on Navigation Controllers

I have a total of 3 views. A menu, the main view where the action happens, and a settings menu.
You can access the settings from both the menu and the main view and go back using the back button provided by the Navigation Controller.
In the main view I have hidden the NavigationBar to free some space, and there's a specific button to go back to the menu. From what I know and have read, I assume this just adds more and more views to the Navigation Stack if I keep going from the main view to the menu again and again, creating a lot of views in the stack.
I'd like someone to tell me whether my assumption is true or not, and evt. explain me the whole process behind navigating and views.
UINavigationController has a property viewControllers which is the stack of view controllers that have been pushed there.
If you use push segues in your storyboard each time you trigger this segues you push the current controller to the stack.
If you have a special logic I suggest you manage controllers programmatically.
This might clear it all.
There are basically following types of Segues to navigate to any viewController
Show (Push)
Show Detail (Replace)
Present Modally
Present as Popover
And to move back use Unwind Segue
You can read more regarding this here

Benefits from using UiNavigationController

I am developing an iOS app that I have already developed for Android.
The problem is I don't know how to organize my UIViewControllers considering the following scheme of my app pages:
The scheme is simple: there is a login page which leads to the main page. From this main page, there are four buttons which all lead to a specific view hierarchy but at the very bottom of each, the user will be able to go back directly to the main page. Each page accessed by the main page will also have a custom back button (an image of my own)
The question is: is there any benefit in using a UINavigationController (obviously with the main page as its root) in my case? Or can I simply create each Controller and using only Modal Segues?
If your view controllers have a navigation relationship so using UINavigationController is the way to go:
In 'push' segue, you are basically pushing your ViewController into an
already setup "navigation stack". Well, of course, this is under the
assumption that the ViewController that performs the 'pushing'
operation belongs to the same navigation stack as the ViewController
is pushed into. Generally, you push a ViewController if the pushed
ViewController has some sort of a relationship with the pushing
ViewController. This is very common in applications that has a
NavigationController in its system. A good example for a push segue is
a system where you are displaying a list of contacts. And on tap of a
particular contact, you are pushing a VC that has the corresponding
details of the contact.
Example is real world: list of products => product details => product reviews
If you want to temporary present a view controller and the main focus is your view controller but you need to present another view controller to perform a task like "filter" , "login", adjust "settings" then modal segue is the way to go
In 'modal' segue, there is no stack as such. You are presenting a VC
'modally' over the presentee VC, if that makes sense. This can happen
across any ViewController without any relationship rules. The
presenter should take care of dismissing the VC it presented. A good
example for modal segue is login. On tap of login, you are modally
presenting a VC that has no relationship with the presenter.
If your view controllers are not related to each other, but each view controller has his own navigation stack then UITabBarController is the way to go
Storyboards (Xcode): What is the difference between a push and modal segue?
I would say if each of the additional view controllers from the main "home" view controller don't have any children view controllers, then you can just have each button present a view controller modally.
The main difference is if you are using a navigation controller, you can "pushing" a vc onto the navigation stack of view controllers, whereas presenting it modally can be thought of a "one time" action where the user does something on the new screen and has no where to advance to logically (like adding information to a new contact).
You can see this post for a more detailed answer:
What is the difference between Modal and Push segue in Storyboards?
Deciding whether to use a Modal segue vs a Show (push) depends entirely on purpose and context of the user's experience. If you are leading the user down a path which is linear, where each successive VC is diving deeper in to a singular idea, then use Show segues and NavigationControllers. Examples include, Settings app, where you can drill into all the specifics. Most e-commerce app will use a NavigationController to lead the user through a purchase.
If you want to present the user with a single concept, which the user can respond to, or close it to continue using the rest of the app. Then use a modal presentation. Adding a contact in the iPhone is a fine example of this.
Visually, the difference is that a Show segue presents the VC from the right side of the app, sliding onto the previous VC. (If the user has Arabic language turned on, a right to left language, the Show segue will come from the left hand side of the VC) A modal comes from the bottom of the app.
From looking at your drawing, but not know anything else about your app, I think you want to use NavigationControllers. You may also want to consider a TabBarController. If each of these buttons lead the user on various ways of using the app, like mini apps within one big one, then a TabBarController is appropriate.

iOS move to segue without adding ViewController to stack

In my application i'm using the navigation controller as an "Action Bar" and in most scenarios there is no Back Icon.
If i understand correctly, when i'm using segues to move to the next screen, iOS keep a reference of my last screen so i will be able to press back and return to it quickly.
can i have the same segue with moving to next screen, but somehow tell the navigation bar not to save an instance of the the last view controller ? i need that in order to avoid duplicate view controllers in the view controllers stack, and to avoid overloading the memory.

Data disappears when switching between view controllers

I am writing an iPhone application using the storyboards for an initial mockup. The problem I have right now is switching view controllers.
I have a table view controller and another view controller. All I want to do is use a back button to go back to the original screen, and I can do that, except the data disappears. The storyboard that I have is shown below.
I have the Back button going back to the original navigation controller. I have also had it going back to the Card view controller.
I have hard coded some example cells to just see how things look and they show up just fine when I run the simulation. When I click the back button though, it goes back to the All Cards screen and the cells that were there are now gone.
If I need to post some code just ask for what part would be helpful, I have done all of this through storyboards though.
I'm sure it's something stupid I've done, any point in the right direction would be greatly appreciated.
Basically: you pushed where you should have popped.
What you are seeing on the Storyboard does not exist yet. By segue-waying during runtime to a view controller it gets instantiated.
When you segue-wayed during runtime from the Add Card view controller "back" to the Card View Controller - here is what happened: instead of popping the navigation stack all the way back to the Card View Controller you already had, you just instantiated a new Card View Controller and pushed it onto the navigation stack. You could verify that by going all the way back to the original Card View Controller by tapping the back button several times.
What you could do to accomplish your task is this:
Instead of using the Storyboard for your back button use an IBAction in code:
- (IBAction)popToRoot:(id)sender {
[self.navigationController popToRootViewControllerAnimated:YES];
}

Resources