简体   繁体   English

如何在iOS上的按钮操作上显示忙碌指示器?

[英]How to show a busy indicator on button action on iOS?

I want to start busy indicator on button action, I use following code in IB action as: 我想在按钮操作上启动忙碌指示器,我在IB操作中使用以下代码:

 busyIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
 busyIndicator.center = self.view.center;
 [busyIndicator setColor:[UIColor redColor]];
 [self.btnInjuryPrevention addSubview:busyIndicator];
 [self.view addSubview:busyIndicator]; // spinner is not visible until started

[busyIndicator startAnimating];
if(![AppStatus isAppOnline]) {
    noInternetViewController = [[NoInternetViewController alloc] initWithNibName:@"NoInternetViewController" bundle:[NSBundle mainBundle]];
    noInternetViewController.view.tag = CHILD_CONTROLLER_TAG;
    [self.navigationController pushViewController:noInternetViewController animated:YES];
    NSLog(@"Yoy are offline..>!!");
}
else
{
    mInjuryPreventionViewController=[[InjuryPreventionViewController alloc] initWithNibName:@"InjuryPreventionViewController" bundle:nil withHeader:@" Injury Prevention" withId:INJURY_PREVENTION_ID];
    mInjuryPreventionViewController.view.tag = CHILD_CONTROLLER_TAG;
    [self.navigationController pushViewController:mInjuryPreventionViewController animated:YES];
    //[mInjuryPreventionViewController release];
}
// [self removeLoadingView];
[busyIndicator stopAnimating];

I use above code, but busy indicator will not start, So can you tell me it is possible? 我使用上面的代码,但是忙碌指示器将无法启动,那么您能告诉我这是可能的吗?

If you start and stop an animation in a single block, you won't see anything. 如果在单个块中启动和停止动画,则看不到任何内容。 You can think of the startAnimating method to mean "start animating when control returns to the OS." 您可以想到startAnimating方法的意思是“当控件返回到OS时开始动画”。

It's not clear why you need a spinner there. 目前尚不清楚为什么需要在那里的微调器。 Does creating the view controller take a long time? 创建视图控制器需要很长时间吗? Still, if you move the view controller stuff into a new method you could do something like: 不过,如果将视图控制器中的内容移动到新方法中,则可以执行以下操作:

...
// create busy indicator
[busyIndicator startAnimating];
[self performSelector:@selector(createViewController) withObject:nil afterDelay:0]; 

...

// at the end of the createViewController method...
[busyIndicator stopAnimating];

as i understand from your given code firstly you are on some UIViewController and where you are adding your UIActivityIndicatorView and after all you making two condition if-else where you initializing two other UIViewController so in this view(Generated after if-else) how your UIActivityIndicatorView will show as you added it as 根据我从给定代码中了解到的内容,首先是在某个UIViewController上,在其中添加UIActivityIndi​​catorView,然后在两个条件if-else处初始化了另外两个UIViewController,因此在此视图中(在if-else之后生成),您的UIActivityIndi​​catorView将显示为您添加为

    [self.view addSubview:busyIndicator]; // spinner is not visible until started

please define your function executing with if statement.? 请定义您的函数与if语句执行。

Keep Coding 保持编码

As you are starting and stoping the busuIndecator in a single method, without any long processing, so both function executes within milisecons so you are not able to see the busyIndecator on UI. 由于您使用单一方法启动和停止busuInde​​cator,而无需进行任何长时间的处理,因此这两个函数都在milisecons中执行,因此您无法在UI上看到busyIndecator。 To show a busyIndecator or any outlet on UI. 在UI上显示busyIndecator或任何插座。 there should me some time span which is human recognisable. 我应该有一段人类可以识别的时间跨度。 Like: 喜欢:

[busyIndicator startAnimating];
//Do EXTREME PROCESSING!!!
[busyIndicator stopAnimating];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM