iPhone:添加“正在加载”子视图
当我的应用程序中发生某些事情时,我想显示一个简单的加载对话框。我想我只需创建一个新视图,为其添加标签,然后将该视图设置为当前视图的子视图。
这样做时,我什么也没看到!
这是我编写方法的方式:
- (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];
但是当从方法加载它时,它似乎滞后,并且没有显示出来。
I am wanting to show a simple loading dialog when certain things are happening in my app. I figured I would just create a new view, add a label to that, and then set that view to a subView of my current view.
When doing this, I don't see anything!
Here is how I am writing my method:
- (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];
}
Am I doing this completely wrong?
EDIT:
I added it to the viewDidLoad method, and it works how I want:
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];
But when loading it from a method, it seems to lag, and not show up for a bit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管这并不能直接回答您的问题,但我建议您从 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])。
Although this doesn't directly answer your question, I'd recommend grabbing MBProgressHUD from GitHub and using that in place of a static label. Looks better, less code for you to directly maintain, etc. You can find it at http://github.com/matej/MBProgressHUD
The way I use it is by creating a subclass of UITableViewController and defining a handful of methods to show and hide the HUD view. From there, I call each relevant method when I'm loading or done loading.
Specifically, I have four methods: -hudView, -showLoadingUI, -showLoadingUIWithText:, and -hideLoadingUI.
-hudView creates a new MBProgressHUD object if one doesn't already exist, and adds it to the current view ([self.view addSubview:hudView]).
-showLoadingUI calls -showLoadingUIWithText: with a default title, -showLoadingUIWithText: just unhides the MBProgressHUD and sets a label value for it (self.hudView.labelText = @"foo";).
-hideLoadingUI hides the hudView ([self.hudView hide:YES]).
首先,我不认为 UIView 有名为
init
的方法。你可以直接称其为super。您应该调用的适当方法是- (id)initWithFrame:(CGRect)aRect
。框架就是要显示的View的位置、大小。 更多这里还有就是为什么调用
[super.view addSubView:]
,我觉得应该是self.view
,不是它?First, I don't think UIView has method called
init
. You may just call the super of it. The appropriate method you should call is- (id)initWithFrame:(CGRect)aRect
. The frame is the position, the size of the View you want to display. More hereAnother thing is why you call
[super.view addSubView:]
, I think it should beself.view
, isn't it?