I have nearly 45 UIButtons in my view, i know that the buttons have not been allocated to the memory, somehow the compiler alloc/release it, but i noticed that the device become slower at this view, so… What should i do to avoid memory leek with the UIButton ??
Here is how i put my buttons into my view:
in myView.h:
UIButton *btn1;
in myView.m :
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setTitle:@"btn1" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
btn1.backgroundColor = [UIColor clearColor];
btn1.frame = CGRectMake( arc4random() % 920, arc4random() %600+50 , 65, 65);
[self.view addSubview:btn1];
and how i remove it:
for(UIButton* b in [self.view subviews]){
[b removeFromSuperview];
b = nil;
}
Memory leaks by themselves will not produce a significant slow-down of your app. Their effect is filling up the memory and, if in sufficient number, the operating system will kill your app due to excessive memory use. (If the operating system did not kill your app, then you could have a slow down, but since it kills the app, no issue).
In any case, if you are worried by memory leaks, you could fire Instruments and see whether it detects any memory leaks when your view is displayed.
Possibly the reason why you view is slow is that is has to load into memory 45 images, reading them from disk (which is slow). You might think of pre-loading them, and see if this improves things.
An easy way to preload an image is to instantiate it through
imageNamed:You could call that method for all of your buttons and collect all the relevant calls in another method that you could call, e.g., at startup, or at any other moment when it makes sense for you.
EDIT:
Just a note: you do not need to remove the buttons from their superview (in normal conditions): this will be done by the framework when removing the superview. Could be this that is slowing down the app?
Besides, if the problem happens when exiting/entering the view, could you try and “cache” the whole view (like: instantiating it just once, then displaying it when it is needed).