Release Dynamic Objects at a later state - ios

I have the following question.
Lets say i have a UIViewController Class named "ModuleViewController" which contains multiple dynamically created UIButtons and multiple dynamically created Mpmovieplayercontrollers. Each of these objects have their Callback functions, contained in the "ModuleViewController" Class. One Call Back for all the UIButtons and one for all The Mpmovieplayercontrollers.
Now i want to add multiple "ModuleViewController" Class instances inside a UIScrollview.
I do not use ARC. If i release these objects after i allocate them and initialize them inside my "ModuleViewController" Class, the Buttons and the Videos do not play or rhe Application Crashs.
Currently my solution is to have an NSMutableArray to store their pointers when they are created, and then release them when i release the "ModuleViewController" Class at a later state. (e.x. Release the "ModuleViewController" Class when it is off screen from the viewport of the UIScrollview)
For example. If my array which holds the pointers is "objectsRetained"
// Creating the Array of fPointers on ViewDidLoad
NSMutableArray *objectsRetained = [[NSMutableArray alloc] init];
.
.
// Add Object Pointer inside the array to release it at later state
[objectsRetained addObject:[NSValue valueWithPointer: myObject]];
This solutions works but when i analyze my application, it shows that there is a posible Memory Leak in this area.
Is there another way to solve this?

First, use ARC.
But in this case, the problem seems to be in your extra NSValue wrapper. Just put the buttons in an NS(Mutable)Array directly. The array will retain the buttons for you. In -dealloc, call [buttons release]. That will release the buttons. There is no need to bundle these things into value objects.
And then switch to ARC. (But the approach is completely the same; you just don't need dealloc then.)

Related

How to change an image using an array in xcode (objective C)

So I'm new Objective C and I'm building my first app, although I'm stuck on a particular function for my character creation feature.
I have added 'back' & 'forward' buttons and these are meant to move between the selections that I have (e.g. shirt images).
I have 4 different choices for the shirt and was wondering how I would add these 4 images into an array that I would then be able to use with the 'back' and 'forward' buttons in order to navigate between the choices.
Any help would be appreciated.
Are you using Swift or Objective-C? I'll assume Objective-C. Ok. So you got it. Now you just got to implement it. You'll have to initialize a new array with the four images, arrayWithObjects: or initWithObjects:. You'll have to have a UIImageView drawn on the screen, added as a subview (UIView's addSubview:). Then you may want to keep a disposable "counter" private ivar (could be a property too if you wanted) that keeps track of the index of the current image being displayed (declared in your .m, .h is fine too). You could do this or you could have a dynamic getter method that returns the index based on the method objectAtIndex: on your array with your image view's image. When the user clicks forward or back the counter is updated (if it's an ivar and not a getter) and the image view's image is updated based on the new counter value (eg. self.imageView.image = imageArray[counter]) or something like that. You could disable the forward/back buttons if the counter value is on the edge of the array's count property. When the view is initially loaded you could load the image view's image based on the current counter value which would be 0 by default.
In this answer, I did not write the code for you. I showed you snippets of stuff and gave you different potential alternatives to write your program. You'll have to put it together yourself and choose what you're most comfortable with.
In Objective-C, there are two classes of objects: Primitives (like NSInteger, int, char) and everything else. Non primitives can be stored in NSArrays. NSArrays are immutable, meaning that you cannot change them once they have been created. However, there is a sub-class of NSArrays called NSMutableArray. You can add and delete objects to these and sort them if you want. So if you have four
UIImage objects, you could do something like this:
NSMutableArray* images = [NSMutableArray new];
[images addObject:image1];
[images addObject:image2];
[images addObject:image3];
[images addObject:image4];
....
The objects are added in order, so you can retrieve the second image (for example) like this:
UIImage* second = [images objectAtIndex:1];
(Indexing is zero-based)

Array of UITextView's that interact with objects of the other classes

I'm rather new to objective C and at the moment I'm trying to create one small project.
The task I want to accomplish is the following:
I've got the UIViewController for the screen of the game I'm creating. It has an UIImageView and a UITextView on it. What it does so far is that the latter one is moving towards the former one. And when their frames intersect (CGRectIntersectsRect) some actions happen.
What I want to achieve next is to create a specific class for UITextviews, so that there will be many of them created on the screen of UIViewController (I think array should be used here). And next I want every of them to be checking themselves, if they have an intersection with the UIImageView - than (again) something happens.
I've tried several ways like creating a mutable array, but every time I've some errors connected with variables of the original ViewController used inside of the new class (Hit).
The code I use for the one existing UITextView, that is created inside of UIViewController, is the following:
-(void)Moving{
HitR.center = CGPointMake(HitR.center.x+HitRX, HitR.center.y+HitRY);
if (CGRectIntersectsRect(HitR.frame, Finish.frame)) {
/*some actions here*/
}
}
etc
Can you help me to create these array of UItextFields, using their own class, tell them what to do with the help of properties like UIimageview.frame from the ViewController and then to place them on the screen.
P.S. I've read numerous articles about how to transfer variables from one class to another, but still failed to accomplish my aim.

UIImageView best practices

I have always wondered what are the best practices while working with UIImageView objects and a will give you a few examples about what I'm unsure.
First of all I am working on a turn based game that supports multiple matches at the same time, and certain views (a background image, label and a few buttons) will be loaded to self.view very often. What is the best way to display them, add them and then remove:
[self.view addSubview:view];
[view removeFromSuperView];
Or is the best way to add them and play with the hidden property (show and hide whenever i need, even in different matches)?
Another question is do I need to set an UIImageView to nil after I remove it from superview?
And the last question is: If I have a UIView class that I load to an UIImageViewController and want to release/remove it from within [self removeFromSuperView] is enough to release all the memory occupied by that view class?
If those views are loaded often into the screen, the best approach is to hide them instead of removing them. I'll remove them when I'm not using them anymore.
Removing a view from the superview reduces the retain count of the object in 1. If you are using ARC, you shouldn't worry about it, if you are not, be sure that the retain count is 0 after removing it (+1 for every alloc, add to subview, and -1 for every release, autorelease, removeFromSuperView). If after removing the view the retain count still 1, you can do = nil to release it. If the retain count of an object is 0, then the system will free it.
Same as 2.
Regarding points 2 and 3, it really depends on the scope of your UIImageView. If you declared it as a strong property, then you will have to set it to nil in order for ARC to release the memory. If it just a variable inside a method, then at the end of the execution of the method body, the variable will be released anyway (and be retained only by the view hierarchy).

UIButton released from memory?

I create a couple of UIButtons (with selectors) in a method (throughout the life cycle of the app) and stick them all into an NSMutableArray. The created buttons are NOT properties but the array is.
If I delete a button from the array, does the button automatically get released from memory?
If the answer to the above is no, what do I have to do to get it released from memory?
The button will indeed, eventually, be released from memory as no one is retaining it. Its retain count will be decremented regardless, but only if no one else is retaining it, it will be released from memory.
This is however a somewhat confusing mechanism - keep in mind that the UIButtons are views and are probably a part of your view hierarchy which also retains them. If they're also visible, they must be removed from their superview in order to be released.
Anything you put in a NSArray (and a NSMutableArray) gets retained by the array, and released when removed from the array or when the array itself is released. It's true for buttons and any other object.

iOS Design Pattern memory management with delegate and parent object

Im struggling to solve in a very clean way a problematic involving memory overload (management).
Im having a serie of view that include other views, in my project I have a situation like this:
MainView
|_PageView
|_CustomButton
soo far soo good, easy as a cake. CustomButton have a delegate (protocol) in it for some reasons, so we have in PageView a "for cycle" that creates N CustomButtons, set the delegate as self in PageView (PageVew extend CustomButtonDelegate) and release the buttons afer attaching them like
{
CustomButton *customButton_ = [[CustomButton alloc] initWithFrame:CGRectMake(100.0,50+(i*55.0),200.0);
customButton.delegate = self;
[self addSubView:customButton_];
[customButton_ release];
}
soo far soo good again. Button will be press, PageView get the protocol method, do some code and voilĂ , done. One problem is that at one point, MainView must remove PageView, so In a method I call
[pageView_ removeFromSuperview];
[pageView release], pageView_ = nil;
pageView_ = [PageView alloc] initWithFrame.....];
and I recreate the object with other data to display.
I noticed that PageView never gets release and removed from the memory because its retainCount is exactly how many CustomButton I created inside PageView and assign the delegate to self plus one of course. My question is, what is the cleanest way to remove safely all the objects and be able to remove PageView too, free the memory (because Im loading a quite large amount of data to display in it) ?
Right now i'm doing:
Create in PageView a NSMutableArray, that I CustomButton the objects in
it, and before to remove PageView, I cycle it and set the delegate = nil and then release
each object, after I release the NSMutableArray (called "holder").
But the problem is that if I want to add more objects of different types with other protocols, adding to this array, can lead to other problems of retaining the objects.
Where do I lack guys, knowledge so I need to study more (quite sure I can say) or do I need to approach with another OOD?
Thank you guys, im going overload with this problem and my brain is stuck in a close road. :)
Looks like your CustomButton's delegate is a retain property of CustomButton. Delegate should be an assign property, not retain nor copy. See here.

Resources