UIActivityIndicatorView proper usage

2019-08-12 14:09发布

问题:

My question regards the use of activity indicator in an iPhone project. I have a class that contains an UIActivityIndicatorView

@interface StatusView : UIView 
{
    UIActivityIndicatorView *indicator;
    UILabel *textLabel;

}

- (id)initWithFrame:(CGRect)frame Text:(NSString*)text andShowIndicator:(BOOL)value;

In my business logic code I call [indicator startAnimating] and the frame appears at the bottom of the screen. The code also contains a dealloc method that releases the indicator

- (void)dealloc 
{
    [indicator release];

    [super dealloc];
}

Most of the time the indicator works well, however there are a few occasions that never disappears.

Do I always need to explicitly call the stopAnimating method? Does the release handle it? What is the proper usage?

回答1:

stopAnimating: method stops the wheel of UIActivityIndicatorView and release release the object.
In Objective-C each object has an internal counter that is used to keep track of all references used by the objects or object has. [object retain] increments the counter by 1 and [object release] decrements the counter by 1. When counter reaches to zero, dealloc is then called. release is about memory management while stopAnimating: is a functionality of UIActivityIndicatorView. So if you want to stop animating your UIActivityIndicatorView you would have to call stopAnimating: method. In ARC dont have to do release so better to use ARC.



回答2:

the best way when you are using this object is stopAnimating when you want to stop, remove from super view ( [activityObject removeFromsuperview] ) and finally release it. [ activityObject release];