I have a UITabBar + UINavigationController application which often needs data from the internet. Sometimes it takes quite a while before it gets it, so I would like to show an activity indicator.
我有一个UITabBar + UINavigationController应用程序,它经常需要来自互联网的数据。有时需要很长时间才能得到它,所以我想展示一个活动指标。
What I was trying is to add a activityView to my window in my applicationDidFinishLaunching method:
我尝试的是在我的applicationDidFinishLaunching方法中向我的窗口添加一个activityView:
[window addSubview:tabBarController.view];
fullscreenLoadingView.hidden = YES;
[window addSubview:fullscreenLoadingView];
And then I add the application delegate as a observer to the default notification center:
然后我将应用程序委托作为观察者添加到默认通知中心:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startFullscreenLoading:) name:@"startFullscreenLoading" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopFullscreenLoading:) name:@"stopFullscreenLoading" object:nil];
and implement the methods:
并实现方法:
- (void)startFullscreenLoading:(NSNotification *)notification {
fullscreenLoadingView.hidden = NO;
}
- (void)stopFullscreenLoading:(NSNotification *)notification {
fullscreenLoadingView.hidden = YES;
}
When I then use this directly in the applicationDidFinishLaunching method the loading indicator view shows upp as expected:
当我在applicationDidFinishLaunching方法中直接使用它时,加载指示器视图按预期显示upp:
[[NSNotificationCenter defaultCenter] postNotificationName:@"startFullscreenLoading" object:self];
But when I use it from one of the navigation controllers the startFullscreenLoading: method is called but I don't see the loading indicator view. Why is that?
但是当我从其中一个导航控制器使用它时,调用了startFullscreenLoading:方法,但是我没有看到加载指示器视图。这是为什么?
3 个解决方案
#1
Are you loading your data on a background thread? Your main thread will be blocked & won't be able to redraw or process any other events.
您是否在后台线程上加载数据?您的主线程将被阻止,无法重绘或处理任何其他事件。
#2
The most likely thing I can guess is that your indicator view is below some other view. Try
我猜测的最可能的事情是你的指标视图低于其他视图。尝试
[[fullScreenLoadingView superView] bringSubviewToFront:fullScreenLoadingView]
[[fullScreenLoadingView superView] bringSubviewToFront:fullScreenLoadingView]
If that doesn't work, I would suggest breaking inside of -startFullscreenLoading
to make sure the fullscreenLoadingView
is still valid.
如果这不起作用,我建议打破-startFullscreenLoading内部以确保fullscreenLoadingView仍然有效。
#3
In my app I did this by adding the UIActivityView in IB and making sure it was above everything else (as a top-level object). It's set to be invisible when stopped.
在我的应用程序中,我通过在IB中添加UIActivityView并确保它高于其他所有内容(作为*对象)来完成此操作。它被设置为在停止时不可见。
Then in my code I make it appear with [activityIndicator startAnimating] and make it disappear with [activityIndicator stopAnimating];
然后在我的代码中,我使用[activityIndicator startAnimating]使其显示并使其与[activityIndicator stopAnimating]一起消失;
#1
Are you loading your data on a background thread? Your main thread will be blocked & won't be able to redraw or process any other events.
您是否在后台线程上加载数据?您的主线程将被阻止,无法重绘或处理任何其他事件。
#2
The most likely thing I can guess is that your indicator view is below some other view. Try
我猜测的最可能的事情是你的指标视图低于其他视图。尝试
[[fullScreenLoadingView superView] bringSubviewToFront:fullScreenLoadingView]
[[fullScreenLoadingView superView] bringSubviewToFront:fullScreenLoadingView]
If that doesn't work, I would suggest breaking inside of -startFullscreenLoading
to make sure the fullscreenLoadingView
is still valid.
如果这不起作用,我建议打破-startFullscreenLoading内部以确保fullscreenLoadingView仍然有效。
#3
In my app I did this by adding the UIActivityView in IB and making sure it was above everything else (as a top-level object). It's set to be invisible when stopped.
在我的应用程序中,我通过在IB中添加UIActivityView并确保它高于其他所有内容(作为*对象)来完成此操作。它被设置为在停止时不可见。
Then in my code I make it appear with [activityIndicator startAnimating] and make it disappear with [activityIndicator stopAnimating];
然后在我的代码中,我使用[activityIndicator startAnimating]使其显示并使其与[activityIndicator stopAnimating]一起消失;