How do I show and start animating a UIActivityIndicatorView from within a method

前端 未结 1 1531
南旧
南旧 2021-01-21 14:55

I have a method that does a time consuming operation, say something like ten consecutive calls to [[NSString alloc] initWithContentsOfURL:u];

I want a UIActivityIndicat

相关标签:
1条回答
  • 2021-01-21 15:49

    I assume you are doing some expensive work in this method and while that work is being performed, you want the activity indicator to spin. Expensive work should NOT be done on the main thread (iOS might kill your app!). Put your expensive work on a separate thread with:

    - (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg
    

    ...and when the method (aSelector) is done, call:

    - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait 
    

    ...and there you stop the activity indicator.

    Never call any UI code from within a non-main thread!

    0 讨论(0)
提交回复
热议问题