質問

My application sends data to the server after you knomki while is transmitting data to the server indicator activity. When the indicator is hidden, I'm trying to display the alert that the data was successfully sent. But when I try to withdraw the alert, the application falls. In what could be the problem? Thank you!

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

役に立ちましたか?

解決 2

The crash occurs because you are trying to display the UIAlertView from a background thread.

Never do that, all UI changes should be handled from main thread.

Replace:

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

With:

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

他のヒント

Reason why your app is crashing because you are trying to deal with your GUI elements i.e UIAlertView in background thread, you need to run it on the main thread or try to use dispatch queues

Using Dispatch Queues

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

    dispatch_async(queue, ^{

     //show your GUI stuff here...

    });

OR you can show the GUI elements on the main thread like this

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

You can have more detail about using GUI elements on Threads on this link

Try to

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top