ios - How can i create a activity indicator in middle of view programmatically? -
i working app parses feed online. when click refresh button takes time re parse file , show data. want activity indicator in middle of view when click refresh button. , when parsing done indicator should hide. using code not working.
- (ibaction)refreshfeed:(id)sender { uiactivityindicatorview *spinner = [[uiactivityindicatorview alloc] initwithactivityindicatorstyle:uiactivityindicatorviewstylewhitelarge]; [self.view addsubview:spinner]; [spinner startanimating]; // parsing code code [spinner release]; }
a uiactivityindicator needs placed separate thread long process (parsing feed) in order appear.
if want keep in same thread, need give indicator time appear. this stackoverflow question addresses placing delay in code.
edit: 2 years later, think time using delay make happen doing wrong. here how task now:
- (ibaction)refreshfeed:(id)sender { //main thread uiactivityindicatorview *spinner = [[uiactivityindicatorview alloc] initwithactivityindicatorstyle:uiactivityindicatorviewstylewhitelarge]; [self.view addsubview:spinner]; //switch background thread dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_background, 0), ^{ //back main thread ui call dispatch_async(dispatch_get_main_queue(), ^{ [spinner startanimating]; }); // more on background thread // parsing code code //back main thread ui call dispatch_async(dispatch_get_main_queue(), ^{ [spinner stopanimating]; }); }); }
Comments
Post a Comment