Jörn Buitink Jörn Buitink - 3 months ago 23
iOS Question

dismissViewControllerAnimated only works after tapping TableView Row second time

In my iOS App, i present a UItableViewController using

...
[self presentViewController:vc animated:YES completion:nil];
...


Now after i tap a row in my tableview, i want to dismiss my tableviewcontroller:

#pragma mark UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self dismissViewControllerAnimated:true completion:^(void) {
NSLog(@"dismissed");
}];
}


Now my problem:
dismissViewControllerAnimated works as intended, but I have to tap the row 2 times. The first time i tap the row nothing happens.

Observations so far:


  • I tap a row one time: Nothing happens

  • I tap a row 2 times: TableviewController dismisses, "dismissed" gets logged

  • I tap a row one time and dismiss the Tableview by tapping somewhere outside the modal: "dismissed" gets logged



Seems like the completion block will be "scheduled" even if I tap only one time. But the dismissing doesn't happen.

Does anyone know what cause this problem?

Answer

Thanks to par and ShahiM I got the solution: I had to dismiss on the Main Thread.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSelectorOnMainThread:@selector(dismissAndshowPdf) withObject:nil waitUntilDone:NO];
}

- (void) dismissAndshowPdf {
    [self.presentingViewController dismissViewControllerAnimated:true completion:^(void) {
         NSLog(@"dismissed");
    }];
}
Comments