Question

hi I’m trying view the activity indicator by clicking the button. I have tried but i having some issues. If i drag the activity indicator to my storyboard but its visible while running my app i want to display while clicking the button. in my app I’m giving the photo upload option. So if they click the upload button i want to view the activity indicator . ..

this is code i have tired but its not working.

this is my h file where i have implemented the activity indicator and the button..

- (IBAction)click:(id)sender;
 @property (strong, nonatomic) IBOutlet UIActivityIndicatorView *spinner;

this is code which i have used in my m file...

-(void)temp
{
 spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
 spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
 [spinner setCenter:CGPointMake(480/2.0, 128.0/2)]; // (mid of screen) I do this because I'm in landscape mode
 [self.view addSubview:spinner];
 [spinner startAnimating];
 [spinner release];
}

and im calling this function in my button..

- (IBAction)click:(id)sender {
[self temp];
}

and also im have some other issues i saw the code here the problem is

[yourView addSubview:spinner]; 

in yourview i give my uiviewcontroller name but its giving error i have changed to

[self.view addSubview:spinner];

pls tell me where im doing wrong and what is the right way to make it.. thanks

Was it helpful?

Solution

Man I got the problem...I think you are adding white activity indicator on white view that's why it is not being display but it is there for sure so...use some other style UIActivityIndicatorViewStyleGray

-(void)temp
{
 spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
 spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
 [spinner setCenter:CGPointMake(480/2.0, 128.0/2)]; // (mid of screen) I do this because I'm in landscape mode
 [self.view addSubview:spinner];
 [spinner startAnimating];
 [spinner release];
}

OTHER TIPS

First of all since you already have an IBOutlet for the spinner so do not allocate a new one in temp method. Modify your temp method to look like this:

  -(void)temp
    {
      spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
     [spinner setCenter:CGPointMake(480/2.0, 128.0/2)]; // (mid of screen) I do this because I'm in landscape mode
     [self.view addSubview:spinner];
     [spinner startAnimating];
    }

I f you are using interface builder like Storyboard or XIB then just set the frame and evrything there itself. And if you want to change the property of it at runtime just use the IBOutlet you have used. You need not to instantiate again just use its getter and setter to use it accordingly. So here you can do.

- (IBAction)click:(id)sender 
  {
    [self.spinner startAnimating];
  }

and synthesize spinner before using it.

And if you want to use it programmatically delete it from XIB and remove all references corresponding to it. And declare in .h file

UIActivityIndicatorView *spinner;

or you can make a property

@property(nonatomic, strong)UIActivityIndicatorView *spinner;

and just use your own code.

Assuming your spinner connected to your view. In your viewDidLoad you can setup your view, then it'll show when you tap the button:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    self.spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
    [self.spinner setCenter:CGPointMake(480/2.0, 128.0/2)]; // (mid of screen) I do this because I'm in landscape mode
    self.spinner.hidden = YES;
}

- (IBAction)click:(id)sender
{
    self.spinner.hidden = NO;
    [self.spinner startAnimating];

     // Additionally you can show spinner in top bar
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)stopSpinner
{
    if (self.spinner.isAnimating) {
        self.spinner.hidden = YES;
        [self.spinner stopAnimating];

        // And hide it when you don't need it anymore
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    }
}

I suggest you see this answer: How to get orientation-dependent height and width of the screen? to re position your spinner.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top