使用 UIActivityIndi​​catorView 的这段代码是否有缺陷?

发布于 2024-11-14 22:08:22 字数 708 浏览 2 评论 0原文

使用 UIActivityIndi​​catorView 的这段代码是否有缺陷?看来我实际上根本没有在这里看到指示器/旋转器,这是因为视图在 who viewDidLoad 完成之前才绘制吗?

解决这个问题的唯一方法是在单独的线程上执行 viewDidLoad 自定义工作(例如数据更新)吗? (我希望在这种情况下有更简单的单线程操作)。有没有办法强制视图在“startAnimating”行之后或者在数据加载开始之前刷新?

UITableViewController 实现的代码:

- (void)viewDidLoad {
    // Wait indicator - Start
    self.waitView = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
    self.waitView.hidesWhenStopped = true;
    [self.view addSubview: self.waitView];

    // Load data into tableview
    [NSThread sleepForTimeInterval: 5.0];   // Test code to simulate

    [self.waitView stopAnimating];
}

Is this code using UIActivityIndicatorView flawed? It appears that I don't actually get to see the indicator/spinner at all here, so is this because the view isn't drawn until the who viewDidLoad completes?

Is the only way around this to do the viewDidLoad custom work (e.g. data updates) on a separate thread? (I was hoping in this case for an easier single-thread operation). Is there a way to force the view to refresh after the "startAnimating" line perhaps prior to the data loading commencment?

Code from UITableViewController implementation:

- (void)viewDidLoad {
    // Wait indicator - Start
    self.waitView = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
    self.waitView.hidesWhenStopped = true;
    [self.view addSubview: self.waitView];

    // Load data into tableview
    [NSThread sleepForTimeInterval: 5.0];   // Test code to simulate

    [self.waitView stopAnimating];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙与沫 2024-11-21 22:08:22

您还应该调用 startAnimating。睡觉不是一个好主意。我更喜欢使用performSelector方法来启动一个不重复的NSTimer。

试试这个:

-(void) doStuff:(id)aSender
{
    [self.waitView stopAnimating];
}
-(void)viewDidLoad
{
    ...
    [self performSelector:@selector(doStuff:) withObject:self afterDelay:5.0];
}

另外:还设置 ActivityIndi​​catorView 的框架或边界属性,就像 sosborn 在他的评论中所说的那样

You should also call startAnimating. Sleeping is not a good idea. I would prefer the performSelector-methods which starts a not recurring NSTimer under the hood.

Try this:

-(void) doStuff:(id)aSender
{
    [self.waitView stopAnimating];
}
-(void)viewDidLoad
{
    ...
    [self performSelector:@selector(doStuff:) withObject:self afterDelay:5.0];
}

in addtion: also set the frame- or bounds-property of the ActivityIndicatorView somewhere like sosborn said in his comment

っ左 2024-11-21 22:08:22

实际上,托马斯的答案应该按原样工作,我将添加一些解释,说明为什么不像您所做的那样使用睡眠。

iPhone(以及大多数操作系统)上的所有 UI 处理仅在一个线程中完成 - 主线程,即执行所谓的运行循环的线程。如果停止该线程,UI 将停止,不会绘制任何内容。

将 sleep 放入在主线程中运行的 viewDidLoad 中即可实现这一目的 - 阻止 UI 执行任何操作。因此,因为在唤醒后您立即调用了 [self.waitView stopAnimating] 并且 ActivityView 在没有动画时应该隐藏,所以您根本看不到它 - 您只是没有给它任何时间展示。

Thomas 使用 NSTimer 在 5 秒后调用 stopAnimating - 现在这可以让主线程在停止动画并隐藏 waitView 之前执行代码,这将适用于您的测试。

更好的是,您只需让它在没有任何计时器的情况下进行动画处理,并在数据加载后使用委托模式由 tableView 加载代码通知,然后停止动画处理。您不知道数据加载将持续多长时间,因此最好等到它完成,而不是在任何特定时间后停止动画。

哦,好吧,大小和位置是有道理的,但对于测试而言,这并不重要,也不是看不到它的原因 - 如果未指定,它将添加到 0,0 处并具有默认大小,因此您会看到无论如何。

Actually the answer from Thomas should work as it is, I will add a little explanation as to why not use sleep as you have done it.

All the UI processing on iPhone (and most of OSs as well) is being done in only one thread - the main thread, the thread that executes the so called run loop. If you stop that thread the UI will stop, nothing will be drawn.

Putting sleep into viewDidLoad, which runs in the main thread, will do just that - stop UI from doing anything. So because immediately after wakeup you've called [self.waitView stopAnimating] and the activityview should hide when not animating, you can't see it at all - you just didn't give it any time to show.

Thomas used a NSTimer to call stopAnimating after 5 seconds - now this lets the main thread to execute code before stopping animation and hiding waitView and this will work for your test.

Better yet you just let it animate without any timer and use a delegate patter to be informed by the tableView loading code after the data has been loaded, then stop animating. You don't know how long loading of data will last, so it's better to wait until it's finished than stop animating after any specific time.

Oh well, and the size and position, makes sense, but for testing it doesn't matter and is not the cause of not seeing it - if not specified it will be added at 0,0 and have a default size so you will see it anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文