Question

In my app I have a class that deals with simple methods, so the main UIViewController will have less code on it. A very strange thing is happening when I'm trying to add and remove a UIActivityIndicator from my UIViewController, to add the UIActivityIndicator there is no problem, but when I try to remove is (or stop it), nothing happens.

This is my code:

This is Called from the main UIViewController:

[LoadingView addLoadingView:self.activityIndicator 
         andTheViewToShowIn:self.view];
[ChooseSongDataSourse reloadArrayWithData:self.arrayPlaylist 
                                andReturn:^(NSString *resone) {
    [LoadingView removeLoadingView:self.activityIndicator 
                andTheViewToShowIn:self.view];
    [self.activityIndicator removeFromSuperview];
}];

And this is done from a separate class("LoadingView"):

+(void)addLoadingView:(UIActivityIndicatorView *)activityIndicator
   andTheViewToShowIn:(UIView *)viewToShowIn{

    activityIndicator = 
      [[UIActivityIndicatorView alloc]
        initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [activityIndicator setCenter:viewToShowIn.center];
    [activityIndicator startAnimating];
    [viewToShowIn addSubview:activityIndicator];
}

+(void)removeLoadingView: (UIActivityIndicatorView *)activityIndicator
  andTheViewToShowIn: (UIView *)viewToShowIn
{
 [activityIndicator removeFromSuperview];
 activityIndicator = nil;
 }

Im not looking for a simple answer, that i can do my self, i'm asking for an explanation way this is happening,

Thanks! (:

Was it helpful?

Solution

+(UIActivityIndicatorView*)addLoadingViewandTheViewToShowIn: (UIView *)viewToShowIn
  {
     UIActivityIndicatorView* activityIndicator = [[UIActivityIndicatorView     alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [activityIndicator setCenter:viewToShowIn.center];
    [activityIndicator startAnimating];
    [viewToShowIn addSubview:activityIndicator];
    [viewToShowIn bringSubviewToFront:activityIndicator];
    return [activityIndicator autorelease];
  }

+(void)removeLoadingView: (UIActivityIndicatorView *)activityIndicator
  andTheViewToShowIn: (UIView *)viewToShowIn
 {
     [activityIndicator removeFromSuperview];
 }

AND:

UIActivityIndicatorView* activityIndicator = [LoadingView addLoadingViewandTheViewToShowIn:self.view];
   [ChooseSongDataSourse reloadArrayWithData:self.arrayPlaylist andReturn:^(NSString *resone) {
      dispatch_async(dispatch_get_main_queue(), ^(){// you can remove it, if you sure that block performed in main-thread
          [LoadingView removeLoadingView:activityIndicator andTheViewToShowIn:self.view];
      });
   }];

The main problem of yours code, that pointer at UIActivityIndicatorView wasn't returned from +(void)addLoadingView:andTheViewToShowIn:;

You can return objects from functions by return parameter or by pointer at pointer (like many functions that return NSError* by taking NSError** argument).

OTHER TIPS

Really simple, Add below methods as class methods in helper

+(UIActivityIndicatorView*)addActivityIndicatorForView: (UIView *)viewToShowIn
{
    UIActivityIndicatorView* activityIndicator = [[UIActivityIndicatorView     alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [activityIndicator setCenter:viewToShowIn.center];
    activityIndicator.autoresizingMask = (UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin);
    activityIndicator.hidesWhenStopped = YES;
    [activityIndicator startAnimating];
    [viewToShowIn addSubview:activityIndicator];
    //[viewToShowIn bringSubviewToFront:activityIndicator];
    return activityIndicator ;



}

+(void)removeActivityIniticator: (UIActivityIndicatorView *)activityIndicator

{
    [activityIndicator removeFromSuperview];
}

then consume like so in client class

//start spinner here
UIActivityIndicatorView *spinner =  [[UtilityHelper sharedInstance] addActivityIndicatorForView:self.view];

//do stuff here

//stop spinner here
        [[UtilityHelper sharedInstance] removeActivityIniticator:spinner];

I am trying to have an activity indicator for two seconds and then to go to the next page.

@property(strong,nonatomic)IBOutlet UIActivityIndicator *activityindctr;

-(void)viewDidload 
{ 
    [super viewDidload];[activityindctr startanimating]; [self performSelector:@selector(nextpage) withObject:nil afterDelay:2];
}

 -(void)nextpage

{ 
    [activityindctr stopAnimating]; [self performSegueWithIdentifier:@"nextviewcintroller" sender:self];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top