Question

In my iPhone app, I want to add activity indicator on top of a searchbar.

When it is searching it should display activity indicator.

I have added the activity indicator in XIB and created its outlet.

I am making it hide when the searching finishes, but Activity Indicator does not display.

Problem

I figured out that search function(say A)(where I animate the activity indicator) in turn calls another function(say B) so the main thread is being used in executing the function B. But for activity indicator to animate we require the main thread.

So I tried calling function B using performSelectorInBackGround:withObject method. Now when I click search the activity indicator is shown but the functionality of function B does not execute.

What can be a work-around for this?

Was it helpful?

Solution 8

I have got the solution and it is as follows.

I just wrote the below line in Search button click event.

[NSThread detachNewThreadSelector:@selector(threadStartAnimating:) toTarget:self withObject:nil];

And defined the function threadStartAnimating: as follows:

-(void)threadStartAnimating:(id)data
{
   [activityIndicator setHidden:NO];
   [activityIndicator startAnimating]; 
}

OTHER TIPS

There is not quite enough in your question to go on, but to start debugging, I would do the following.

  1. Verify that the activity variably is really wired to the UIActivityIndicator you are creating in IB. (I would set a breakpoint on the setHidden: lines and make sure the variable is not null. Or throw an NSAssert(activity,@"Whoops! actity is null"); in there.)

  2. If the variable is indeed set, I would start checking that it is in the right place in the view hierarchy. (I'd try doing a [self.view addSubview:activity] and see that it appears. You might have to replace it somewhere else.)

  3. You might also want to try having it on by default in IB, until you have everything figured out.

Good Luck. Hope this helps.

Save yourself the hassle of creating a custom activity indicator and use the standard one that's available for you already - in the top bar. Also, IMO users tend to expect that one to spin when something is happening.

UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES; 

Obviously, set it to NO when your activity is over.

First of all, make sure you have @synthesize activity at the top of your .m file. Then in the viewDidLoad method, type activity.hidesWhenStopped = TRUE;. Next, in the method that is called when the search starts, type [activity startAnimating]; and [activity stopAnimating]; in the method when the searching stops.

try this:

set hidesWhenStopped = NO, so that is displayed all the time and then hide and show it manually. But the View should be set in IB to hidden first.

- (void)startActivityView {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    activity_view.hidden = NO;
    [pool drain];
}

- (void)stopActivityView {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    activity_view.hidden = YES;
    [pool drain];
}

- (void)doSomething {
    [self performSelectorInBackground:@selector(startActivityView) withObject:nil];
    // do some time consuming work
    [self performSelectorInBackground:@selector(stopActivityView) withObject:nil];
}

Perhaps you have a view in front of your activity indicator? What if you always bring it to the front....

loadView = [[UIActivityIndicatorView alloc] 
            initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
loadView.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
loadView.center = window.center;
loadView.opaque = NO;
[window addSubview: loadView];
[window bringSubviewToFront:loadView];
[loadView startAnimating];

I suggest that you use DSActivityView for showing your activity indicator. The source code can be found at Dejal blog.

Showing, and hiding, the activity view is a simple line of code.

[DSActivityView activityViewForView:self.view];

start animating the activity indicator and with a delay of 0.1 or 0.2 just call the other method u want.... i tried and it is working for me....

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