2013-02-16 47 views
0

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

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

+0

什么记录显示。 – Dilip 2013-02-16 10:39:13

+0

crash log/stack trace plz – 2013-02-16 10:44:06

+0

为什么不尝试在主线程中运行UIAlertView和[activityIndi​​cator stopAnimating]。 – 2013-02-16 10:53:48

回答

1

发生崩溃,因为你试图显示从后台线程的使用UIAlertView线程上的GUI元素的更多细节。

不要这么做,所有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]; 
}); 
3

原因,为什么你的应用程序崩溃,因为你正试图处理你的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

+1

+1为你的快速回答,并击败我:) – 2013-02-16 11:08:17

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]; 
}