iPhone - subview stacking slowing down application? - iphone

As title says, I'm wondering if stacking subviews can slow down an iPhone application.
For example, I have a UIViewController, which has a view occupying the whole screen. When the user presses a button, I create a second view controller and add its view as a subview of the original VC, making the second view completely hide the first one.
Does the application have some kind of automatic optimization which would be something like "ok, I know what to draw for every pixel of the screen, I stop seeking for subviews" ?
If not, I don't think stacking 2 full-screen views can really slow down the app, but could 3, 4 or more views be problematic if they include many subviews themselves (labels, images)?

Read the View Controller Guide sections on Modal View Controllers and memory management. Prefer to use modal Views instead of subviews when you want to present a new screen temporarily and a UINavigationController for "drill-down" views.

You can always set UIView#hidden = YES on the views not seen. That should prevent redraws.

This is largely dependent of what the subviews contain and what is the total memory load of the app. Memory is very crucial for devices like iPhone and you should never keep the things which you don't require. When you are adding many subviews without releasing any, your memory requirement obviously will increase. This may slow down the app, even may crash the app. Stacking of two may not be a problem, but stacking many is not a very good design.
So the summary is you should always check the memory load of the app through instrument and always properly respond to memory warnings.

Related

How many subviews and table views can one view controller handle

I have an app that has an UIScrollView as the main subview of a view controller. User can scroll left/right between different UIViews. And each view will have its own UITableView.
My question is just how many views and table views can one view controller handle before it will get laggy and slow?
Will it be possible to have 10 views and 10 table views and still run smooth on iPhone 4 or should I come up with another way? And if so, how can I improve this?
EDIT:
I have been thinking about using UIPageViewController but I want so have parallax like scrolling between screens (yahoo weather like).
How does Yahoo weather handle so many views?
You can have massive amounts of views and subviews that are all handled by the same controller. Views are not drawn unless they are currently being displayed.
That being said, there are definitely some recycling solutions you can utilize to speed things up if you start noticing performance issues (like removing the memory of a table if it is no longer immediately needed).
The main reason you would start having complications is because of calculations to populate the data. If you have 10 tableviews all of the datasource and delegate methods could get slowed. You could use container views to encapsulate view logic. Also, as mentioned in comments, a UIPageViewController may be a good solution.

iPhone - How prevent an UIWebView to draw/update its content

I have some view inside wich there are an UIWebView. For performance reasons, I do not allocate the Web view on the fly, but it is set into IB and linked with a class outlet. When I do not need it, I set its hidden property to YES.
But, after having profiled my application because of performance problems, I noticed that when resizing the views containing those UIWebViews, they take a huge amount of time in :
[WebFrame(WebInternal) _drawRect:contentsOnly:]
So to prevent this, I also set their scalesPageToFit to NO, but this doesn't change anything.
I've tested deleting them from the project, and yes, the resize of the view is really much faster.
So how can I prevent the UIWebViews that are hidden to eat my process time in useless drawing work ?
Instead of hiding the UIWebView try removing it from the container view using removeFromSuperview. When you want to make it visible again add it back as a subview. This should keep the instance of the UIWebView in memory and prevent it from redrawing when it is not visible.

UIScrollView paging slow animation

I have an application i'm currently developing for iOS which is suffering from some performance issues.
The app takes user input through a navigation controller with 5 views, the last view generates a view with a UIScrollView which has a paging effect.
Each page in this UIScrollView has 16 buttons arranged in a grid, each of these buttons is loaded with an image.
Unfortunately when I scroll to a new page in the UIScrollView, the app stumbles across in a jerky animation rather than a smooth one.
I was wondering if anyone had any suggestions on how to improve the performance for the paging UIScrollView effect.
Many thanks.
Is it smoother the second time you navigate to that view?
If so, it's probably the loading of the images that is taking the time. You can fix that by preloading your button images when the app first loads using [UIImage imageNames:...] in your app delegate. That way they will already be cached and available to render when the view is displayed.
If it's not faster the second time, it's probably the actual rendering of the views that is too slow. You can fix that by preloading the views themselves. If you store a reference to your scrollview's subviews in an array, you can preload those views in your app delegate and just keep them in memory ready to display whenever the scrollview is shown.
I doubt that those views will use up too much memory (probably a couple of MB, max), but if you are worried about it, just add a memory warning handler that flushes out the array and then re-create it next time it's needed.

In a Tab Bar based app a controller release data of the other ! !

I've made a ViewBased app, in the app delegate i've set a UITabBarCotntroller, in the app i have different view Controller two of them displays text in a UITextView and labels, the other one is my "ShakeController" a UIViewController in which i've set a UIAcelerometerDelegate, in it i create a instance of UIAccelerometer, in the method which manages the shake everything works fine, in this controller i have also set a UIImageView to make a simple animation, in the view Did Load method i set my imageView.animation to an array of UIImage.
My problem is : when the app is launched i use the ViewControllers and everything work fine, but when i tap the ShakeController item in the tab bar and then when i come back to the other controllers the label looks like : label and textView like : Lorem ipsum..... the text of UItextView in IB.
I have noticed thaht if i comment the initialisation of my imageView to the array of image i can navigate the items (from a view controller to another) without the label change and stay what i want them to be.
Notice that the two controllers are in a UINavigationController.
(i use #proprety (nonnatomic, retain) then #synthesize ... then releqse in the dealloc for the labels textview and my uiimageView)
Do not know what to do thanks to all
Quite hard to understand what you're asking here... I re-read it three times and I'm still not sure!
Since you find the problem (the data in the other view being "released") goes away when you comment out your imageView animation initialisation, I would conclude that you are using up a large amount of the iPhone's memory which means that it is automatically unloading any un-seen view controllers. It will do this quite quickly to preserve memory.
You don't say how many images are in your animation, but just 10 medium-large images can be enough to trigger memory issues; if you are wanting to use 20/30+ you have to look for another way.
You can try just loading a few frames into your image first and then swapping them out on a recurring timer. But if it's anything more than very basic animation you'll need to get stuck in to core animation instead: http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CoreAnimation_guide/Articles/WhatisCoreAnimation.html#//apple_ref/doc/uid/TP40004689

Making view transitions fast - do you use this hack?

I have an iPhone app that displays a modal view controller. The modal view controller shows two instances of a custom subclass of UITextView called RoundedTextView, an MKMapView, and a UIToolbar. I construct the viewController only once, and reset its data and present it each time the user summons it.
When showing this view controller with presentModalViewController, I noticed that the animation to show the view was choppy on the 3G. So, to speed it up, I set the alpha of the MKMapView and the two RoundedTextView objects to 0 on viewWillDisappear and back to 1 on viewDidAppear. This made it nice and fast. I also presume that I could remove the views from the superview to speed it up as well.
Does anyone else jump through these kind of hoops on the iPhone. Is there something else I should be doing to avoid this hack?
It's not a hack to simplify drawing during animation in order to make the animation more smooth. It is indeed a very valid technique.
You may be able to achieve similar performance improvements by setting all UI elements to Opaque, a technique also used to fix table view cell performance issues. You just have to make sure background colors match.
The main problem I had was I subclassed UIButton to make gradient buttons and I had the boundary mask enabled. This made the performance terrible. I removed that option and made my buttons square and it's blazin now.