Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dismissViewControllerAnimated is not dissmissing ViewController

So....I have a View Controller and when I press a button, another View Controller appears:

- (IBAction)searchButtonPressed:(id)sender {
    [self presentViewController:self.controllerSearch animated:YES completion:nil];
}

Inside view controller number 2 is a table view and when a row is selected in a table this code runs:

NSString *phrase = nil; // Document password (for unlocking most encrypted PDF files)

        NSString *filePath2 = filePath; assert(filePath2 != nil); // Path to first PDF file

        LazyPDFDocument *document = [LazyPDFDocument withDocumentFilePath:filePath2 password:phrase];

        if (document != nil) // Must have a valid LazyPDFDocument object in order to proceed with things
        {
            LazyPDFViewController *lazyPDFViewController = [[LazyPDFViewController alloc] initWithLazyPDFDocument:document];

            lazyPDFViewController.delegate = self; // Set the LazyPDFViewController delegate to self

#if (DEMO_VIEW_CONTROLLER_PUSH == TRUE)

            [self.navigationController pushViewController:lazyPDFViewController animated:YES];

#else // present in a modal view controller

            lazyPDFViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
            lazyPDFViewController.modalPresentationStyle = UIModalPresentationFullScreen;

            [self presentViewController:lazyPDFViewController animated:YES completion:NULL];

#endif // DEMO_VIEW_CONTROLLER_PUSH
        }
        else // Log an error so that we know that something went wrong
        {
            NSLog(@"%s [LazyPDFDocument withDocumentFilePath:'%@' password:'%@'] failed.", __FUNCTION__, filePath2, phrase);
        }

Now I am using LazyPDFKit and it comes with this delegate method:

- (void)dismissLazyPDFViewController:(LazyPDFViewController *)viewController
{
    // dismiss the modal view controller
    [self dismissViewControllerAnimated:YES completion:NULL];

}

I put a break point and I can see my code goes into the delegate method, but the LazyPDFViewController does not go away.

I have tried the following:

[[[self presentingViewController] presentingViewController] dismissViewControllerAnimated:YES completion:nil];

but that takes me back a few view controllers to far.

Am I missing something?

Additional code in my first view Controller .h

@property (strong, nonatomic) UISearchController *controllerSearch;

and in first view controller .m

- (UISearchController *)controller {

    if (!_controllerSearch) {

        // instantiate search results table view
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
        LHFileBrowserSearch *resultsController = [storyboard instantiateViewControllerWithIdentifier:@"SearchResults"];

        // create search controller
        _controllerSearch = [[UISearchController alloc]initWithSearchResultsController:resultsController];
        _controllerSearch.searchResultsUpdater = self;

        // optional: set the search controller delegate
        _controllerSearch.delegate = self;

    }
    return _controllerSearch;
}
like image 211
user979331 Avatar asked Oct 18 '22 12:10

user979331


2 Answers

If you are pushing the view controller:

[self.navigationController pushViewController:lazyPDFViewController animated:YES];

Then the code in the delegate doesn't make sense, because it assumes it is a modal view controller that needs to be dismissed:

- (void)dismissLazyPDFViewController:(LazyPDFViewController *)viewController
{
    // dismiss the modal view controller
    [self dismissViewControllerAnimated:YES completion:NULL];

}

But you've added it to the navigation stack (I assume).

If you can't pop it again from the navigation controller at this point you are missing some code in your example.

Are you sure your delegate is firing on the main thread? Try:

- (void)dismissLazyPDFViewController:(LazyPDFViewController *)viewController
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.navigationController popViewControllerAnimated:YES];
    });
}
like image 80
davbryn Avatar answered Oct 30 '22 18:10

davbryn


try this:

- (void)dismissLazyPDFViewController:(LazyPDFViewController *)viewController
{
    // dismiss the modal view controller
    [[viewController presentingViewController] dismissViewControllerAnimated:YES completion:nil];

}

your code : [[[self presentingViewController] presentingViewController] dismissViewControllerAnimated:YES completion:nil]; just went too far.

like image 38
Allen Avatar answered Oct 30 '22 18:10

Allen