活动指标和警报

时间:2013-02-16 10:37:38

标签: ios android-activity alert indicator

我的应用程序在您将数据传输到服务器指示器活动时将数据发送到服务器。当指示器被隐藏时,我正在尝试显示数据已成功发送的警报。但是当我尝试撤消警报时,应用程序就会崩溃。可能是什么问题?谢谢!

- (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

3 个答案:

答案 0 :(得分:3)

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

使用Dispatch Queues

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元素的更多详细信息

答案 1 :(得分:1)

发生崩溃是因为您试图从后台线程显示UIAlertView

永远不要这样做,所有UI更改都应该从主线程处理。

替换:

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

使用:

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

答案 2 :(得分:1)

尝试

- (IBAction)sendForm:(id)sender {
 [self performSelectorInBackground:@selector(loadData) withObject:activityIndicator];
 [activityIndicator startAnimating];
 UIAlertView* ahtung = [[UIAlertView alloc] initWithTitle:@"Спасибо" message:@"Ваша заявка принята!\nВ течение часа, Вам поступит звонок для подтверждения заказа" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
 [ahtung show];
}