MemoryManagement with UINavigationController - objective-c

I have an "issue" with my NavigationController. Actually it seems to me, that this behavior seems to be intended.
My App uses a SplitViewController, using a TableViewController for the MasterView and a NavigationController for the DetailView.
I have 4 views, which i can switch from each to another. What I observed is that every time I use one of the NavigationController buttons to change the View the memory consumption increases by almost 1.5MB.
Using the back-button of the NavigationController the views seem to get released correctly as the used memory decreases.
I read that the views are put on a stack every time i switch them, but actually that is not what I need (At least I hope so, because at a certain degree I rely on the viewdidload-method to update the content, which I pass via NSNotification to each view. At that point it would be nice if all views could share that passed userInfo, but that's a different story...).
So what I'm looking for is a way to replace the newest ViewController on the NavigationController view stack by the one. I don't need a history of like 100 views where like 40 views always are actually the same. I only found answers about memory not getting released after using the back button. Therefore I assume the current behavior is the intended one.

what I'm looking for is a way to replace the newest ViewController on the NavigationController view stack by the one
setViewControllers:animated: lets you construct the stack however you like.
https://developer.apple.com/library/ios/documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html#//apple_ref/occ/instm/UINavigationController/setViewControllers:animated:

Related

tvOS - dismissing to top of storyboard causes all intermediate screens to -viewWillAppear/Disappear

I am several levels deep in a storyboard and want to unroll everything all the way back to the first screen. Fortunately, there are APIs designed to do exactly this:
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
[topController dismissViewControllerAnimated:NO completion:nil];
However, this method seems to "unroll" the view stack one by one, causing each view between my current position and the first screen to briefly invoke viewWillAppear, viewDidAppear, viewWillDisappear, and viewDidDisappear. This causes a cacophony of activity in my app, as most of the intermediate screens do interesting things when they appear. I can set breakpoints in Xcode and watch these methods get invoked in reverse order back to the main screen.
I need a way to pop back to the start of the storyboard without causing every screen along the path to light up and do work.
If this means I cannot use viewWillAppear for this purpose any more, I am willing to switch to a replacement method as long as one exists.
This is expected behaviour. I assume you're presenting the layers of views with presentViewController... calls on each previous view.
You should look at using a UINavigationController as your top level view instead. Then you can just call popToRootViewControllerAnimated: when you want to go all the way back.

How come my uitextfield(s) keeps being disabled after model?

I made an app for my wife to track inventory. it has a couple of view controllers to enter a new item, display all items, search items, and display selected item. After switching through each view a couple of times (no particular order) all of the textfields become disabled (you cannot select them to change the value) across ALL of the view controllers. The buttons still work however. I have it setup so that when the user enters info into a particular cell, it saves it into memory so that if they switch views and then come back, the cells will get the data in the viewdidload method. Please Help!
*edit - I am using storyboard with a bunch of segues (i'm new to programming and self taught, so idk if this is the right way or not)... here is a pic of my storyboard
http://i.imgur.com/aT3PR0i.png
Ok, with a look at your storyboard, I think I see your problem. If your storyboard looks like somebody spilled spaghetti on it, it's not designed correctly. Specifically, it looks like you're going "backwards" (to controllers you've already been to) using segues -- that's not good. SEGUES ALWAYS INSTANTIATE NEW CONTROLLERS! So, if you go around in a circle a few times, you're adding more and more controllers to your project. To go backwards using segues, you need to use an unwind segue -- it's the only one that is an exception to the rule that segues always instantiate new controllers. The other ways to go backwards, without segues, are popping with navigation controllers, and dismissing modal controllers.
I can't really diagnose your immediate problem without a lot more information, but redesigning the storyboard using unwind segues would probably fix your problem.

Putting an ADBannerView on top of a UINavigationController

I'm trying to put an iAd banner in an app that is based on a UINavigationController (it's not the standard nav-base app proposed by xcode, cause I don't need the table view).
I'd like to place an ADBanner on its bottom, to be always visible, no matter how the user pops and pushes views.
I studied the iAdSuite example in the apple sample code, but, though it's reported among the "best practices", I don't think it's the best practice for what I need. It basically declares an ADBannerView in the app delegate class and then implements the ADBannerViewDelegate methods for every single view the app needs. That means implementing the ADBannerViewDelegate methods over and over again on every view controller class you need! It doesn't seem too smart... :(
I'd rather prefer to have an approach more similar to what Apple itself does in the tab bar based app, where you have a part of the window always occupied by the tab controller and all the views switching above without affecting the tab bar view below.
You can't directly put an ADBannerView along with the nav controller in the app delegate, because ADBanner needs to be placed in a view controller (you get a runtime error otherwise).
I tried to subclass from UIViewController, implementing the ADBannerViewDelegate in this class, and place it in the rootViewController along with a UINavigationController but I'm not having good luck with this approach...
Has anybody found a good, simple way to to this? Any hint?
Thank you for any help...
You can have just one class for ADBannerViewDelegate, and just one instance of ADBanner itself. When the currently active view changes, remove ADBanner from the old view, add it as a subview to the new view.
EDIT:
to clarify, you don't need each view implement the ADBannerViewDelegate. You only should have one class implement it (that class doesn't have to be a view controller for that matter).
You would also need to maintain a somewhere a property that would point to the currently active view (e.g. you can update that property in your Navigation Controller's navigationController:didShowViewController:animated:, or come up with your own protocol for that if your views appear in a more complex way).
Then in your ADBannerViewDelegate you'd just resize the view currently pointed to by that current view property. The actual view doesn't even have to know it has an ad in it ;)

Can I use UITabBarController as a simple viewController switcher?

I'm creating an iPad app based on a UINavigationController (with the bar hidden) so I can push and pop other viewControllers for navigation around the app. However, I am now wanting to add a section in which there are two viewControllers that I want to be able to switch between, so in other words they are side-by-side, rather than hierarchical.
Is it okay to use a UITabBarController for this? I am aware that on the iPhone it is recommended they are used only at the root level of the app, but since this is an iPad app I wondered if I could use it? Also, I guess I need to create an empty viewController, create a UITabBarController within it and set the delegate to it, then add the two viewControllers to it... So in effect I will have a viewController within another viewController, and when I have done that in the past the results have been very flaky.
Can I do it this way? The only other way I can think of doing it is to have two plan UIViews within a UIViewController, but that also means I shouldn't really put any business logic in them (bad MVC!), and not being able to will be a right pain in the a**.
Any ideas?
Thanks!
:-Joe
EDIT: I also need to be able to swipe-animate between the two VCs within the TabBarController, AND have a menubar over the top which doesn't animate... Can I do this?
Sure.
I do this kind of thing all over the place in an app I'm working on. I actually have several different types of "toolbars" that can be optionally shown at different times.
What I do is create a "parent" member in my toolbar's class - and when a button is pressed, I have the toolbar call a method in the parent class to do whatever needs to be done - (i.e. display another view).
This avoids the whole mess of creating a view inside another view (or viewcontroller inside another viewcontroller - or whatever) - the toolbar can take the button hits, but all the views are opened by the root view/controller.
Hope this helps/makes sense!

"Infinite Drill-down" with UINavigationController

Can anyone suggest an article or perhaps an example of how to create an "infinite drill-down" with UINavigationController like you see in the Facebook, IMDB and BrightKite apps?
The UINavigationController is designed to be used this way. If memory becomes an issue, the nav controller will release hidden views and reload them when the time comes to navigate back down the stack. Apple recommends you load your UIViews from a NIB for this reason.
http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/NavigationControllers/NavigationControllers.html#//apple_ref/doc/uid/TP40007457-CH103-SW28
Scroll down to item #4, "Configure the view for your root view controller."
Never having done this... I would approach this by keeping a stack with a light-weight object containing the contents of the previous view (perhaps just a URL?). Rather than just push on a new UIViewController to the UINavigationController, you would pop your current one, without animation, then push on the new view. However, I don't think this would properly handle the nice animation.
Another way that comes to mind would be to manipulation the viewControllers array of UINavigationController. After pushing on a new view, just remove the previous view from the array. That way the UINavigationController stack would only ever be 1 or two elements deep. Handling the back button would create the proper view, insert it into the viewControllers array, then pop off the current one. Your state would be managed by the lightweight object stack, not heavyweight view controllers.
-dan