0

调用pushViewController这里是我的代码...无法获得UIActivityIndi​​cator露面,而在didSelectRowAtIndexPath方法

- (void) threadStartAnimating { 
    [activityIndicator startAnimating]; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    activityIndicator=[[UIActivityIndicatorView alloc] init]; 
    [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    activityIndicator.center=[self tableView].center; 
    [[self tableView] addSubview:activityIndicator]; 

    [NSThread detachNewThreadSelector:@selector(threadStartAnimating) toTarget:self withObject:nil]; 
    EventController *eventController = [[EventController alloc] init]; 
    [eventController setEventID:[[lineItems objectAtIndex:[indexPath row]] objectForKey:@"view_event_button_param"]]; 
    [activityIndicator stopAnimating]; 
    [[self navigationController] pushViewController:eventController animated:YES]; 
} 

事件控制器包含一个Web服务,需要几秒钟的加载。我能得到活动指示器显示的唯一方法是如果我在那里抛出一个无限循环...我发现了一堆提到将它放在一个单独的线程中的例子,但它似乎不起作用。

+0

那么'EventController'从它的'init'方法从web服务加载东西呢?或者其他地方 – mattjgalloway 2012-01-10 22:21:16

+0

是的,eventControllers viewDidLoad方法调用我的方法loadDataIntoView它调用Web服务 – 2012-01-10 22:36:35

+0

然后看到我的答案。 – mattjgalloway 2012-01-10 22:41:48

回答

5

你的问题是,如果你正在获取viewDidLoad中的数据,那么直到加载视图之后才会调用该方法,这通常是在首次显示该视图控制器的过程中。在你的情况下,这是在拨打pushViewController:animated:期间。

所以一个办法,你想可能是交换围绕这两条线是什么:

[activityIndicator stopAnimating]; 
[[self navigationController] pushViewController:eventController animated:YES]; 

但是,在所有诚实,我会创建一个协议称为EventControllerDelegate,有一个弱引用属性的id <EventControllerDelegate>在你的EventController,然后设置视图控制器,它创建EventController作为代表,然后再按下它。然后在委托中定义一个方法并在视图控制器中实现它,并在该方法中停止微调器。然后在EventController中,当您完成加载数据时,请调用委托方法。

编辑:如果你真的想这样做没有定义一个委托,你可以尝试改变你的代码是这样的:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    activityIndicator=[[UIActivityIndicatorView alloc] init]; 
    [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    activityIndicator.center=[self tableView].center; 
    [[self tableView] addSubview:activityIndicator]; 

    EventController *eventController = [[EventController alloc] init]; 
    [eventController setEventID:[[lineItems objectAtIndex:[indexPath row]] objectForKey:@"view_event_button_param"]]; 

    [activityIndicator startAnimating]; 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     UIView *v = eventController.view; 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      [activityIndicator stopAnimating]; 
      [[self navigationController] pushViewController:eventController animated:YES]; 
     }); 
    }); 
} 

它使用GCD清楚和一贯的“视图属性访问迫使一个loadView/viewDidLoad“黑客。

+0

切换这些行不起作用。该指标不会显示。也许是因为没有足够的时间?我希望得到最简单的解决方案。很多我的屏幕需要加载的各种原因,我希望只是粘贴一些代码在每个区域,我初始化下一个viewController – 2012-01-11 00:03:11

+0

嗯,你不能,真的。我真的提出了像我展示的委托方式。 – mattjgalloway 2012-01-11 00:07:09

+0

是否有其他地方我可以移动Web服务代码,这将允许它以我拥有它的方式工作?也许viewWillAppear? – 2012-01-11 00:27:26

-3

您需要为您的activityIndicator设置一个帧大小。

activityIndicator.frame = CGRectMake(x, y, 40, 40); 
+0

不起作用。我不认为这是必要的,因为它会显示如果我投入无限循环 – 2012-01-11 00:04:17

相关问题