iPhone - UIView 不显示

发布于 2024-08-29 20:13:39 字数 889 浏览 6 评论 0原文

我对 UIView 有一个奇怪的问题:

我想显示我用 Interface Builder 创建的活动指示器视图来指示长时间运行的活动。

在我的主要 viewController 的 viewDidLoad 函数中,我像这样初始化 ActivityIndi​​cator 视图:

- (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 技术交流群。

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

发布评论

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

评论(3

关于从前 2024-09-05 20:13:39

您是否尝试过在不同的线程上调用 StartWorking 方法?
也许它的繁重进程会阻止其他指令的发生。

看看 NSThread 类,尤其是 detachNewThreadSelector:toTarget:withObject: 方法。

编辑:关于池问题,如果在不同的线程上调用它,则需要在 StartWorking 方法中创建一个池:

- ( void )StartWorking
{
    NSAutoreleasePool * pool = [ [ NSAutoreleasePool alloc ] init ];

    /* Code here... */

    [ pool release ];
}

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:

- ( void )StartWorking
{
    NSAutoreleasePool * pool = [ [ NSAutoreleasePool alloc ] init ];

    /* Code here... */

    [ pool release ];
}
鸵鸟症 2024-09-05 20:13:39

代替 :
[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];}

懒猫 2024-09-05 20:13:39

好的,我找到了一个基于 santoni 答案的解决方案:

- (IBAction)LaunchButtonPressed{            
    // Show the Activity indicator view.
    [self performSelector:@selector(ShowActivityIndicatorView) withObject:nil afterDelay:0];

    // eavy work 
    [self performSelector:@selector(StartWorking) withObject:nil afterDelay:2];  

    // Hide the loading view.
    [load.view removeFromSuperview];    
}

在调用 eavy 函数之前显示活动指示器视图。

谢谢你的回答。

Ok, I found a solution based on santoni answer :

- (IBAction)LaunchButtonPressed{            
    // Show the Activity indicator view.
    [self performSelector:@selector(ShowActivityIndicatorView) withObject:nil afterDelay:0];

    // eavy work 
    [self performSelector:@selector(StartWorking) withObject:nil afterDelay:2];  

    // Hide the loading view.
    [load.view removeFromSuperview];    
}

The Activity Indicator view is dislayed before the call to the eavy function.

Thank's for answering.

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