[英]iPhone: Add “loading” subView
当我的应用程序中发生某些事情时,我想显示一个简单的加载对话框。 我想我只是创建一个新视图,在其中添加标签,然后将该视图设置为当前视图的subView。
这样做时,我什么也看不到!
这是我编写方法的方式:
- (void)showLoading {
UIView *loading = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
loading.backgroundColor = [UIColor blackColor];
UILabel *txt = [[UILabel alloc] initWithFrame:CGRectMake(198, 9, 94, 27)];
txt.text = @"Loading...";
txt.textColor = [UIColor whiteColor];
[loading addSubview:txt];
[super.view addSubview:loading];
[super.view bringSubviewToFront:loading];
[loading release];
[txt release];
}
我这样做是完全错误的吗?
编辑:我将其添加到viewDidLoad方法,它可以按我的方式工作:
loading = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
loading.backgroundColor = [UIColor blackColor];
UILabel *txt = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 94, 27)];
txt.text = @"Loading...";
txt.textColor = [UIColor whiteColor];
[loading addSubview:txt];
[txt release];
[self.view addSubview:loading];
[self.view bringSubviewToFront:loading];
但是,从方法加载它时,它似乎滞后了,并且没有出现。
尽管这不能直接回答您的问题,但是我建议您从GitHub上获取MBProgressHUD并使用它代替静态标签。 看起来更好,可以直接维护的代码更少,等等。您可以在http://github.com/matej/MBProgressHUD中找到它
我使用它的方式是通过创建UITableViewController的子类并定义一些方法来显示和隐藏HUD视图。 在加载或完成加载后,从那里调用每个相关方法。
具体来说,我有四个方法:-hudView,-showLoadingUI,-showLoadingUIWithText:和-hideLoadingUI。
-hudView创建一个新的MBProgressHUD对象(如果尚不存在),并将其添加到当前视图([self.view addSubview:hudView])。
-showLoadingUI调用-showLoadingUIWithText:带有默认标题,-showLoadingUIWithText:仅取消隐藏MBProgressHUD并为其设置标签值(self.hudView.labelText = @“ foo”;)。
-hideLoadingUI隐藏hudView([self.hudView hide:YES])。
首先,我不认为UIView具有称为init
方法。 您可以称其为超级。 您应该调用的适当方法是- (id)initWithFrame:(CGRect)aRect
。 框架是您要显示的视图的位置和大小。 这里更多
另一件事是为什么您调用[super.view addSubView:]
,我认为它应该是self.view
,不是吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.