如何在点击手势上显示活动指示器

时间:2011-08-22 07:08:25

标签: iphone objective-c

我想在点击手势上显示活动指示器,并且还从当前viewcontroller导航到下一个viewcontroller我已经编写了关于tap手势方法的代码,我的代码是 -

- (void)tapToAutoFocus:(UIGestureRecognizer *)gestureRecognizer
{
    textLabel.hidden = YES;
    indicator  = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(140, 150, 30, 30)];
    [indicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:indicator];
    [indicator startAnimating];

    AccountAnfordernViewController *accountAnfordernViewController = [[AccountAnfordernViewController alloc]init];
    [self.navigationController pushViewController:accountAnfordernViewController animated:YES];
    [accountAnfordernViewController release];
}

它导航到下一个视图控制器,但指示器没有显示,我想当它导航到nextviewcontroller时,指示器也应该显示一段时间,然后导航到下一个视图控制器。我该怎么做?

1 个答案:

答案 0 :(得分:0)

我怀疑你需要做这样的事情:

- (void)tapToAutoFocus:(UIGestureRecognizer *)gestureRecognizer
{
    textLabel.hidden = YES;
    indicator  = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(140, 150, 30, 30)];
    [indicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:indicator];
    [indicator startAnimating];
    [self performSelector:@selector(pushAccountAnfordern) withObject:nil afterDelay: 0.1];
}

- (void) pushAccountAnfordern;
{
    AccountAnfordernViewController *accountAnfordernViewController = [[AccountAnfordernViewController alloc]init];
    [self.navigationController pushViewController:accountAnfordernViewController animated:YES];
    [accountAnfordernViewController release];
}

您可以改变延迟。事实上,0.0可能就好了。这种方式允许当前运行循环结束,使得微调器有机会在开始下一位代码之前开始显示。对于强制微调器在当前运行循环结束之前启动的另一种方法(具有潜在的副作用),这个SO question