在 IBAction 方法返回后显示 UIView

Posted

技术标签:

【中文标题】在 IBAction 方法返回后显示 UIView【英文标题】:UIView is shown after IBAction method returns 【发布时间】:2016-08-06 08:12:29 【问题描述】:

我有以下代码片段(用于说明目的):

- (IBAction)buttonPress:(id)sender 
    UIView *overlayView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    overlayView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    activityIndicator.center = overlayView.center;
    [overlayView addSubview:activityIndicator];
    [self.navigationController.view addSubview:overlayView];
    //[self.navigationController.view bringSubviewToFront:overlayView];
    [activityIndicator startAnimating];

    sleep(10);

    //[overlayView removeFromSuperview];

不知道为什么只有方法返回后才会显示覆盖视图...

【问题讨论】:

从哪里返回 @MikeAlter 来自堆栈 :) 【参考方案1】:

我猜这是因为sleep(10) 阻塞了负责执行 UI 相关活动(例如在屏幕上绘图)的主线程。

删除sleep(10) 行,应该可以正常工作。

如果您对如何在 coca-touch 中使用延迟感兴趣,请点击这里的链接:Adding delay between execution of two following lines

【讨论】:

对...那么在方法中使用同步操作是个坏主意。

以上是关于在 IBAction 方法返回后显示 UIView的主要内容,如果未能解决你的问题,请参考以下文章