Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 987
Jörn Buitink Avatar asked Feb 08 '23 10:02

Jörn Buitink


2 Answers

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");
    }];
}
like image 98
Jörn Buitink Avatar answered Feb 10 '23 23:02

Jörn Buitink


Call deselectRowAtIndexPath before dismissing controller

like image 34
Igor Palaguta Avatar answered Feb 10 '23 22:02

Igor Palaguta