草庐IT

iphone - UIAlertView 迟到了

coder 2024-01-25 原文

我在我的 iOS 应用程序中与服务器通信。我有以下方法可以打开警报 View 。我想在应用从服务器获取响应时显示加载 View 。

- (void) showDetailedQuestion:(id)sender
{
       //loading view                  
        self.loading_alert = [[UIAlertView alloc] initWithTitle:@"Loading\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
        [self.loading_alert show];

        UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] 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);
        [indicator startAnimating];
        [self.loading_alert addSubview:indicator];

    UIButton *btn = (UIButton*)sender;
    int indx = btn.tag;

    NSLog(@"tag:%d",indx);
    answerAnQuestion *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"Answer"];

    vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal ;
    vc.que_id = [self.que_id valueForKey:[NSString stringWithFormat:@"%d",indx]];
    vc.qid_list = self.que_id;
    vc.crnt = indx;

    [self presentViewController:vc animated:YES completion:nil];
    [self.loading_alert dismissWithClickedButtonIndex:0 animated:YES];
}

在另一个 answerAnQuestion.m 中

- (void)viewDidLoad
{
            NSString *address = [NSString stringWithFormat:@"%@%@%@%@%@%@%@", path,@"questions/",que_id,@"?token=",token,@"&user_id=",usrId];
    NSURL *URL = [NSURL URLWithString:address];
    NSLog(@"%@",address);

    [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[URL host]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLCacheStorageAllowedInMemoryOnly
                                                       timeoutInterval:60.0];
    [request setHTTPMethod:@"GET"];
    responseData = [[NSMutableData alloc] init];

    NSURLResponse *response = nil;
    NSError *error = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

    if (data) 
    {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response;
        //If you need the response, you can use it here
        int statuscode = [httpResponse statusCode];
        NSString *responseMsg = [NSHTTPURLResponse localizedStringForStatusCode:statuscode];
        NSLog(@" Status code: %d",statuscode );
        NSLog(@" Status msg: %@",responseMsg );

    }
    else 
    {
        // Handle error by looking at response and/or error values
        NSLog(@"%@",error);
    }
}

我的问题是 alertview 仅在 View 更改时显示片刻。它应该在我单击按钮时打开。可能是什么原因?如何解决?

编辑 1:

如果我向服务器发出异步请求,那么我将无法在我的表格 View 中设置这些数据。仅当发送同步请求时,我才能在我的 TableView 中设置这些数据,但它会阻止该应用程序。为什么会这样?

任何帮助将不胜感激。 谢谢你。

最佳答案

您正在主线程上发送 SynchronousRequest,因此它阻塞了您的 UI 线程。阅读多线程,您将获得有关此的各种教程。我可以建议您选择 GCD 或 NSOperation 和 NSOperationQueue。谷歌以上任何一项,您将获得相同的各种样本。

或者你可以像下面这样发送异步请求...

[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] 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.
 }];

更新:

//Display alert view, before sending your request..
[alertview show];
//send first request
[NSURLConnection sendAsynchronousRequest:request1 queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){

            //Write code you want to call when data is received,
            //send second request
[NSURLConnection sendAsynchronousRequest:request2 queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){

            //Write code you want to call when data is received,
            //send third request
[NSURLConnection sendAsynchronousRequest:request3 queue:[[NSOperationQueue alloc] 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... 
});
     }];
     }];

     }];

关于iphone - UIAlertView 迟到了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18531487/

有关iphone - UIAlertView 迟到了的更多相关文章

  1. iphone - 扩展 restful_authentication/AuthLogic 以支持匿名 iPhone 的延迟登录的最佳方法是什么? - 2

    我正在构建一个与RubyonRails后端对话的iPhone应用程序。RubyonRails应用程序还将为Web用户提供服务。restful_authentication插件是提供快速和可定制的用户身份验证的绝佳方式。但是,我希望iPhone应用程序的用户在新列中存储一个由手机的唯一标识符([[UIDevicedevice]uniqueIdentifier])自动创建的帐户。稍后,当用户准备好创建用户名/密码时,帐户将更新为包含用户名和密码,iPhone唯一标识符保持不变。用户在设置用户名/密码之前不能访问该网站。然而,他们可以使用iPhone应用程序,因为该应用程序可以使用它的标识符

  2. iphone - 设计和 Rails 3 中的 http 身份验证 - 2

    我有一个使用deviseonrails3的应用程序。我想启用http身份验证,以便我可以从iPhone应用程序向我的网络应用程序进行身份验证。如何从我的iPhone应用程序进行身份验证以进行设计?这安全吗?还是我应该进行不同的身份验证? 最佳答案 从设计的角度来看,您有3个选择:1)使用基本的http身份验证:您的iPhone应用程序有一个secretkey-这是在您的iPhone应用程序代码中烘焙的-用于对网络应用程序的每个请求进行身份验证。Google搜索:“设计基本的http身份验证”2)您可以通过在您的iPhone应用程序中

  3. iphone - 在没有 Mac 的情况下开发 iPhone 应用程序? - 2

    这个问题在这里已经有了答案:关闭13年前。PossibleDuplicates:HowcanIdevelopforiPhoneusingaWindowsdevelopmentmachine?我想为我妻子的手机构建一个iPhone应用程序,但我对购买Mac作为一次性工作的开发平台不感兴趣。应用程序:应该在iPhone上独立运行(即没有网络连接)完全可以接受使用iPhoneJavascript库之一创建的GUI会做一些数据库IO来读取和更新数据没有商业值(value),永远不会被任何人使用这是我的想法:越狱iPhone在iPhone上安装Ruby+Sinatra使用Sinatra编写应用程

  4. iphone - iPhone 原生应用的测试驱动设计 - 2

    我正在试验iPhoneSDK并在Nic博士的rbiPhoneTest项目中做一些TDD。我想知道有多少人(如果有的话)成功地使用了这个或任何其他iPhone/Cocoa测试框架?更重要的是,我想知道如何最好地断言专有的二进制请求/响应协议(protocol)。这个想法是通过网络发送二进制请求并接收二进制响应。请求和响应是使用byteand'ing和or'ing创建的。我正在使用黄金副本模式来测试我的请求。这是我到目前为止所拥有的。不要笑,因为我是ObjectiveC和Ruby的新手:requireFile.dirname(__FILE__)+'/test_helper'require'

  5. iphone - 如何从视频中提取方向信息? - 2

    在网络上浏览了大量文档后,iPhone似乎总是以480x360的纵横比拍摄视频,并在视频rails上应用变换矩阵。(480x360可能会改变,但对于给定设备而言始终相同)这是一种在iOS项目中修改ffmpeg源代码并访问矩阵http://www.seqoy.com/correct-orientation-for-iphone-recorded-movies-with-ffmpeg/的方法这是在iOS-4中查找转换矩阵的更清晰的方法Howtodetect(iPhoneSDK)ifavideofilewasrecordedinportraitorientation,orlandscape.

  6. ruby-on-rails - 在 env.rb 之外需要 Cucumber-rails。其余的加载被推迟到 env.rb 被调用 - 2

    请问这个env.rb错误是什么意思?root#rakedb:migrateWARNING:Cucumber-railsrequiredoutsideofenv.rb.Therestofloadingisbeingdefereduntilenv.rbiscalled.Toavoidthiswarning,move'gemcucumber-rails'underonlygroup:testinyourGemfilegemfile在这里:source'http://rubygems.org'gem'rails','3.1.0'#BundleedgeRailsinstead:#gem'rail

  7. javascript - 是否允许将javascript代码下载到iPhone - 2

    我有一个混合应用程序,它基本上是一个从UIWebview内的应用程序文件夹运行的网站。问题是我打算通过从互联网下载整个网站然后替换旧网站来更新我的网站。今天发现苹果现在提供了AppStoreReviewGuidelines等规则:2.7Appsthatdownloadcodeinanywayorformwillberejected2.8Appsthatinstallorlaunchotherexecutablecodewillberejected因为我的网站有html、css和javascript,这是否意味着我的应用程序将被拒绝或有机会被接受?你对此有何看法?

  8. javascript - 拖动以像在 iPhone 中一样使用 jQuery 更改图像幻灯片 - 2

    我正在为我的站点使用超大型jquery插件。它带有下一张和上一张幻灯片的按钮。我想在其中实现拖动更改功能。如果有人点击并将鼠标向右移动,它应该充当下一张幻灯片按钮。但是我如何使用jquery来实现呢?我怎么知道用户何时单击n向左/向右拖动http://buildinternet.com/project/supersized/ 最佳答案 jQuery将mouseUp、mouseDown和mouseMove识别为事件。您必须在mouseDown上捕获鼠标位置,在它移动时更新位置并将其与您最初捕获的位置进行比较。虽然这是高度理论化的,但请

  9. javascript - iPhone 为 (hash and 3) 和 (Asterisk and 8) 返回相同的按键事件 - 2

    我正在处理电话验证,需要使用电话号码自动格式化输入,并且只允许添加数字字符。但是,当我尝试使用keydown和keypress限制输入时,iPhone允许我输入#和*。当我检查keydown值时,它们分别与3和8相同(键码51和56)。这在Android浏览器中完美运行,但在iPhone中失败。任何人都遇到过类似的问题。$(formSelector+'input[name^="phone"]').on('keydownkeypress',function(e){//Allow:backspace,delete,tab,escape,andenterif(e.keyCode==46||e

  10. javascript - iPhone 应用程序和移动 safari 之间是否有任何共享状态? - 2

    我有一个网站和一个本地iPhone应用程序。该应用程序注册了一个自定义协议(protocol)。我希望网站在适当的时候自动重定向到协议(protocol),但前提是用户安装了应用程序(以避免烦人的对话框)。这意味着我需要从应用程序中写入一些我可以在移动safari中读取的状态,以将应用程序标记为已安装。Cookie似乎不存在跨进程。还有其他地方可以存放我的标记吗? 最佳答案 看this博客文章,了解Apple如何为MobileMeGallery应用程序做到这一点。它涉及应用程序在Safari中打开一个网站(在您的服务器上),该网站将

随机推荐