iphone - UIAlertView 出现晚了
<p><p>我正在与我的 ios 应用程序中的服务器通信。我有以下打开警报 View 的方法。我想在应用程序从服务器获取响应时显示加载 View 。</p>
<pre><code>- (void) showDetailedQuestion:(id)sender
{
//loading view
self.loading_alert = [ initWithTitle:@"Loading\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
;
UIActivityIndicatorView *indicator = [ initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
// Adjust the indicator so it is up a few pixels from the bottom of the alert
indicator.center = CGPointMake(loading_alert.bounds.size.width / 2, loading_alert.bounds.size.height - 50);
;
;
UIButton *btn = (UIButton*)sender;
int indx = btn.tag;
NSLog(@"tag:%d",indx);
answerAnQuestion *vc = ;
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal ;
vc.que_id = ];
vc.qid_list = self.que_id;
vc.crnt = indx;
;
;
}
</code></pre>
<p>在另一个 answerAnQuestion.m</p>
<pre><code>- (void)viewDidLoad
{
NSString *address = ;
NSURL *URL = ;
NSLog(@"%@",address);
];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLCacheStorageAllowedInMemoryOnly
timeoutInterval:60.0];
;
responseData = [ init];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = ;
if (data)
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response;
//If you need the response, you can use it here
int statuscode = ;
NSString *responseMsg = ;
NSLog(@" Status code: %d",statuscode );
NSLog(@" Status msg: %@",responseMsg );
}
else
{
// Handle error by looking at response and/or error values
NSLog(@"%@",error);
}
}
</code></pre>
<p>我的问题是警报 View 仅在 View 更改时显示片刻。它应该在我单击按钮时打开。可能是什么原因?如何解决?</p>
<p>编辑 1:</p>
<p>如果我向服务器发出异步请求,那么我无法在我的 tableview 中设置这些数据。我只能在发送同步请求时在我的表格 View 中设置这些数据,但它会阻止应用程序。为什么会这样? </p>
<p>任何帮助将不胜感激。
谢谢你。 </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您在主线程上发送 SynchronousRequest,因此它阻塞了您的 UI 线程。阅读多线程,您将获得有关此的各种教程。我可以建议你选择 GCD 或 NSOperation 和 NSOperationQueue。谷歌搜索上述任何一个,你都会得到不同的样本。</p>
<p>或者你可以如下发送异步请求...</p>
<pre><code> init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
//Write code you want to call when data is received,
//Like dismissing loading view and populating UI.
}];
</code></pre>
<p>更新:</p>
<pre><code>//Display alert view, before sending your request..
;
//send first request
init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
//Write code you want to call when data is received,
//send second request
init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
//Write code you want to call when data is received,
//send third request
init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
//Write code you want to call when data is received,
//dismiss alert view on main thread
dispatch_async(getmainqueue, ^(void) {
// dismiss alert view...
});
}];
}];
}];
</code></pre></p>
<p style="font-size: 20px;">关于iphone - UIAlertView 出现晚了,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/18531487/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/18531487/
</a>
</p>
页:
[1]