Any ideas on firing off a "loading" UIView when a particular item in a UITabBarController is selected?
I have an app that has four tabs. I have four UINavigationControllers, each of which is bound to the UITabBarController by setting the TabBarItem property of each controller and then adding the contr开发者_如何转开发ollers to the UITabBarController. That's all standard iOS fare.
One of these tabs loads up a UITableView that displays some data from a remote web service. Because the web service calls routinely take about 1.5 seconds to complete and deserialize, I'd like to display a "loading" UIView with the text "Loading..." when the corresponding tab is selected. I've tried this a couple of ways:
assigning a delegate to the ViewControllerSelected event on the UITabBarController, and checking to make sure that the controller selected is the one I'm interested in. Then I try to show the "loading" UIView. This all happens in AppDelegate;
overriding the ViewWillAppear and ViewWillDidAppear methods on the controller itself. Show the LoadingHUDView in the ViewWillAppear method and hide in the ViewDidAppear method.
In both cases:
- The "loading" UIView is a local variable (lhv) and is already setup with the text I want.
- lhv.SharedApplication.KeyWindow.Add(lhv);
- lhv.StartAnimating(); (the UIView contains a UIActivityIndicatorView)
- Perform the service calls and refresh the UITableView
- lhv.StopAnimating();
- lhv.RemoveFromSuperview();
(And yes, I am aware that I don't need to use lhv.SharedApplication.KeyWindow in AppDelegate because I have the local "window" member available to me.)
The problem is that the "loading" UIView doesn't render until AFTER the UINavigationController has rendered its view. I want the LoadingHUDView to appear IMMEDIATELY, as soon as the UITabBarController is tapped. I've tried using InvokeOnMainThread() as well, but no luck there.
Any ideas?
>.< Figured it out. Just had to spin up the loading UIView in a new thread at the beginning of ViewWillAppear or ViewDidAppear. Then just have to do the cleanup at the end of the respective method by removing the view from the superview. Should've thought of that one a long time ago! D'oh!
declare spinner separatly; and add MyView.addsubview(_spinner);
精彩评论