2013-02-16 35 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]; 
}