Search code examples
iphoneobjective-ccocoa-touchios4uiactivityindicatorview

Activity Indicator when integrated into Searchbar does not display in iPhone SDK


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?


Solution

  • 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]; 
    }