iPhone - UIView 不显示
我对 UIView 有一个奇怪的问题:
我想显示我用 Interface Builder 创建的活动指示器视图来指示长时间运行的活动。
在我的主要 viewController 的 viewDidLoad 函数中,我像这样初始化 ActivityIndicator 视图:
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
load = [[ActivityIndicatorViewController alloc] init];
...
当我按下按钮时,它会调用 IBAction :
- (IBAction)LaunchButtonPressed{
// Show the Activity indicator view.
[self.view addSubview:load.view];
// eavy work
[self StartWorking];
// Hide the loading view.
[load.view removeFromSuperview];
}
在 StartWorking 函数中,我向互联网服务器发出请求并解析它返回给我的 XML 文件。
问题是,如果我调用 StartWorking 函数,应用程序不会通过显示“活动指示器”视图启动,而是通过 StartWorking 函数启动。 而如果我删除对 StartWorking 函数的调用,则会显示视图。
有人能解释我为什么吗? :s
I have a strange problem with a UIView :
I want to show an Activity Indicator View that I created with Interface Builder to indicate long running activity.
In the viewDidLoad function of my principal viewController I init the ActivityIndicator View like this :
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
load = [[ActivityIndicatorViewController alloc] init];
...
When I push a button it call this IBAction :
- (IBAction)LaunchButtonPressed{
// Show the Activity indicator view.
[self.view addSubview:load.view];
// eavy work
[self StartWorking];
// Hide the loading view.
[load.view removeFromSuperview];
}
In the StartWorking function, I ask a request to an internet serveur and parse the XML file that it return me.
The problem is that if I Call my StartWorking function, the application do not start by showing the Activity Indicator view but with the StartWorking function.
Whereas if I remove the call to StartWorking function, the view is shown.
Is someone able to explain me why? :s
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过在不同的线程上调用 StartWorking 方法?
也许它的繁重进程会阻止其他指令的发生。
看看 NSThread 类,尤其是 detachNewThreadSelector:toTarget:withObject: 方法。
编辑:关于池问题,如果在不同的线程上调用它,则需要在 StartWorking 方法中创建一个池:
Have you tried to call the StartWorking method on a different thread?
Maybe its heavy process prevents other instructions to take place.
Look at the NSThread class, especially the detachNewThreadSelector:toTarget:withObject: method.
EDIT: About the pool problem, you need to create a pool in your StartWorking method, if it's called on a different thread:
代替 :
[self.view addSubview:load.view];
使用:
[self PerformSelector:@selector(addLoadingSubview) afterDelay:0.1f];
并创建方法:
-(void)addLoadingSubview{[self.view addSubview:load.view];}
Replace :
[self.view addSubview:load.view];
With :
[self performSelector:@selector(addLoadingSubview) afterDelay:0.1f];
And create the method :
-(void)addLoadingSubview{[self.view addSubview:load.view];}
好的,我找到了一个基于 santoni 答案的解决方案:
在调用 eavy 函数之前显示活动指示器视图。
谢谢你的回答。
Ok, I found a solution based on santoni answer :
The Activity Indicator view is dislayed before the call to the eavy function.
Thank's for answering.