I want to create a similar class to UIAlertView which doesn't require a strong ivar.
For example, with UIAlertView, I can do the following in one of my UIViewController's methods:
UIAlertView *alertView = [[UIActionSheet alloc] initWithTitle:nil
message:@"Foo"
delegate:nil
cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
[alertView show];
... and the actionSheet will not be dealloced until it is no longer visible.
If I were to try to do the same thing:
MYAlertView *myAlertView = [[MYAlertView alloc] initWithMessage:@"Foo"];
[myAlertView show];
... the myAlertView
instance will automatically be dealloced at the end of the current method I am in (e.g. right after the [myAlertView show]
line).
What is the proper way to prevent this from happening without having to declare myView as a strong
property on my UIViewController? (I.e. I want myView to be a local variable, not an instance variable, and I would like the MYAlertView instance to be in charge of its own lifecycle rather than my UIViewController controlling its lifecycle.)
Update: MYAlertView inherits from NSObject, so it cannot be added to the Views hierarchy.
You need to retain it somehow until it is released.
I do not really understand why you cannot implement it as subclass of
UIView
. Then you could use the view hierarchy as the keeper of a strong reference (retain +1). But you will have good reasons for not doing so.If you don't have such a thing then I would use an
NSMutableArray
as class varialbe (meaning statc). Just declare it in the@interface
block and initialize it with nil:provide an accessor.
Within the init method do the following:
You will invode some method when the alert is dismissed.
You may want to add some stuff to make it mutli threading save, which it is not. I just want to give an example that explains the concept.
allMyAlert could be an
NSMutableSet
as well. No need for an array as far as I can see. Adding the object to an array or set will add 1 to the retain count and removing it will reduce it by 1.I've done my own AlertView with a little trick.
Just retain the object himself and release it on action. With this, you can call your custom alert vies as native one.
If you add it as a subview of another view it will be retained. When the user selects and action or dismisses it, then it should call self removeFromSuperview as it's last act.
UIAlertView
creates aUIWindow
, which it retains. The alert view then adds itself as a subview of the window, so the window retains the alert view. Thus it creates a retain cycle which keeps both it and its window alive.UIActionSheet
works the same way.If you need your object to stay around, and nothing else will retain it, it's fine for it to retain itself. You need to make sure you have a well-defined way to make it release itself when it's no longer needed. For example, if it's managing a window, then it should release itself when it takes the window off the screen.