ios - Activity 指示器和警报

标签 ios android-activity alert indicator

我的应用程序在您 knomki 将数据传输到服务器指示器 Activity 后将数据发送到服务器。当指示器隐藏时,我试图显示数据已成功发送的警报。但是当我试图撤销警报时,应用程序失败了。可能是什么问题?谢谢!

- (void)viewDidLoad
{
    [super viewDidLoad];

    activityIndicator = [[UIActivityIndicatorView alloc] init];
    activityIndicator.frame = CGRectMake(140, 190, 37, 37);
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
    [self.view addSubview:activityIndicator];

}


- (IBAction)sendForm:(id)sender {
 [self performSelectorInBackground:@selector(loadData) withObject:activityIndicator];
    [activityIndicator startAnimating];
}

-(void)loadData {
    NSURL *scriptUrl = [NSURL URLWithString:@"http://zav333.ru/"];
    NSData *data = [NSData dataWithContentsOfURL:scriptUrl];
    if (data)
    {
        NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://zav333.ru/sushi.php"]
                                                               cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                           timeoutInterval:15.0];
        request.HTTPMethod = @"POST";
        // указываем параметры POST запроса
        NSString* params = [NSString stringWithFormat:@"name=%@&phone=%@&date=%@&howmuchperson=%@&smoke=%@ ", self.name.text, self.phone.text, self.date.text, self.howmuchperson.text, self.smoke];
      *howMuchPerson, *smoke;
        request.HTTPBody =[params dataUsingEncoding:NSUTF8StringEncoding];

        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        [activityIndicator stopAnimating];

            UIAlertView* ahtung = [[UIAlertView alloc] initWithTitle:@"Спасибо" message:@"Ваша заявка принята!\nВ течение часа, Вам поступит звонок для подтверждения заказа" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [ahtung show];
    }
}

enter image description here

最佳答案

您的应用程序崩溃的原因是您试图在后台线程中处理您的 GUI 元素,即 UIAlertView,您需要在主线程上运行它或尝试使用调度队列

使用调度队列

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

    dispatch_async(queue, ^{

     //show your GUI stuff here...

    });

或者您可以像这样在主线程上显示 GUI 元素

[alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];

您可以在此 link 上获得有关在线程上使用 GUI 元素的更多详细信息

关于ios - Activity 指示器和警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14909353/

相关文章:

ios - 如何将数据从 View Controller 传递到选项卡栏 Controller 的第一个选项卡

ios - XCode UITests 无法运行并出现意外退出错误(仅限机器人)

android - fragment 中的 runOnUiThread

android - 将列表传递给 Android 中的另一个 Activity

ios - 钛加速器 : Links/Images in Labels/TextArea - Open in WebView

java - 通过绑定(bind)服务从服务到 Activity 进行通信

javascript - 在 JavaScript 中显示警报消息时避免页面提交

javascript - Cookies不会通过javascript命令执行提示和警报

javascript - 替换警报窗口

ios - 如何使用 iOS 状态恢复来恢复 UIPopoverController?