调用网络服务时显示警报

发布于 2024-10-21 03:21:11 字数 641 浏览 7 评论 0原文

我有以下代码。

UIActivityIndicator *activity = [[UIActivityIndicator alloc] initWithActivityIndicatorStyle:
                                                    UIActivityIndicatorStyleWhite];


UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Processing" delegate:self otherButtonTitles:nil];
   [alert addSubview:activity];    
   [activity startAnimating];    
   [alert show];

WebServiceController *web = [[WebServiceController alloc]init];

NSDictionary *dict = [web getDetails];

问题是警报没有显示。 WebServiceController 是一个 XML 解析器,它从指定的 URL 获取详细信息并返回它们。应在调用服务时显示警报,因为获取详细信息需要时间。但它仅在服务呼叫结束后才显示警报。这是为什么?

i have the following code.

UIActivityIndicator *activity = [[UIActivityIndicator alloc] initWithActivityIndicatorStyle:
                                                    UIActivityIndicatorStyleWhite];


UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Processing" delegate:self otherButtonTitles:nil];
   [alert addSubview:activity];    
   [activity startAnimating];    
   [alert show];

WebServiceController *web = [[WebServiceController alloc]init];

NSDictionary *dict = [web getDetails];

The problem is that the alert is not getting displayed. The WebServiceController is an XML parser which gets details from a specified URL and returns them. The alert should be shown while the service is called as it takes time to get the details. But it displays the alert only after the service call is over. Why is that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

薄荷→糖丶微凉 2024-10-28 03:21:11

因为[alert show]需要动画,由于服务控制器调用发生在主线程上,主线程正忙于执行服务调用,阻塞了alert view动画的执行。

您需要在后端线程上执行ServiceCall,请参阅NSOperation或PerformSelectorOnBackgroundThread,确保将具有AlertView的ViewController的委托传递给后端线程,服务调用完成后立即回调该委托。确保使用performSelectorOnMainThread 在主线程上执行回调调用。所有与 UI 相关的调用都应在主线程上执行。

because [alert show] will require animation, since the service controller call is taking place on the main thread, the main thread is busy executing the service call, blocking the alert view animation to execute.

You need to perform the ServiceCall on backend thread, see NSOperation or PerformSelectorOnBackgroundThread, make sure you pass in delegate of the ViewController that has the AlertView to the backend thread, callback the delegate as soon as the service call is completed. Make sure you perform the call for the callback on the mainthread using performSelectorOnMainThread. All UI related calls should be executed on the main thread.

心在旅行 2024-10-28 03:21:11

添加到上面的帖子中,您必须像这样编写警报:

UIActivityIndicator *activity = [[UIActivityIndicator alloc] initWithActivityIndicatorStyle:
                                                    UIActivityIndicatorStyleWhite];


UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Processing" delegate:self otherButtonTitles:nil];
   [alert addSubview:activity];    
   [activity startAnimating];    
   [alert show];

[self performSelectorInBackground:@selector(doTheWork) object:nil];

您需要声明一个函数 (doTheWork),它将处理 Web 服务调用,如下所示:

-(void)doTheWork {

WebServiceController *web = [[WebServiceController alloc]init];

NSDictionary *dict = [web getDetails];

[alert dismissWithClickedButtonIndex:0 animated:YES]; //dismissing the alert

}

Adding to the above post, you have to write the alert like this:

UIActivityIndicator *activity = [[UIActivityIndicator alloc] initWithActivityIndicatorStyle:
                                                    UIActivityIndicatorStyleWhite];


UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Processing" delegate:self otherButtonTitles:nil];
   [alert addSubview:activity];    
   [activity startAnimating];    
   [alert show];

[self performSelectorInBackground:@selector(doTheWork) object:nil];

You need to declare a function (doTheWork) which will take care of the web service call as follows:

-(void)doTheWork {

WebServiceController *web = [[WebServiceController alloc]init];

NSDictionary *dict = [web getDetails];

[alert dismissWithClickedButtonIndex:0 animated:YES]; //dismissing the alert

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