1

我对 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 函数中,我向 Internet 服务器发出请求并解析它返回给我的 XML 文件。

问题是,如果我调用我的 StartWorking 函数,应用程序不会通过显示 Activity Indicator 视图而是使用 StartWorking 函数来启动。而如果我删除对 StartWorking 函数的调用,则会显示视图。

有人能解释我为什么吗?:秒

4

3 回答 3

2

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

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

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

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

    /* Code here... */

    [ pool release ];
}
于 2010-04-21T09:39:03.537 回答
1

代替 : [self.view addSubview:load.view];

和 : [self performSelector:@selector(addLoadingSubview) afterDelay:0.1f];

并创建方法: -(void)addLoadingSubview{[self.view addSubview:load.view];}

于 2010-04-21T10:08:31.737 回答
0

好的,我找到了一个基于 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];    
}

Activity Indicator 视图在调用 eavy 函数之前显示。

谢谢回答。

于 2010-04-21T10:29:33.593 回答