Resolving ViewControllerAnimated rejection does not override ViewController

So ... I have a view controller, and when I click the button, the following view controller appears:

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

Internal view controller number 2 is a table view and when a row is selected in the table in which this code is executed:

 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 set a breakpoint, and I see that my code goes into the delegate method, but LazyPDFViewController does not disappear.

I tried the following:

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

but this returns me several view controllers.

Did I miss something?

Extra code in my first view .h controller

 @property (strong, nonatomic) UISearchController *controllerSearch; 

and in the first view of the 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; } 
+5
source share
5 answers

try the following:

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

+1
source

If you click a view controller:

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

Then the code in the delegate does not make sense, since it assumes that it is the modal view manager that needs to be fired:

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

But you added it to the navigation stack (I suppose).

If you cannot repeat it again from the navigation controller, then you are missing the code.

Are you sure your delegate is shooting at the main stream? Try:

 - (void)dismissLazyPDFViewController:(LazyPDFViewController *)viewController { dispatch_async(dispatch_get_main_queue(), ^{ [self.navigationController popViewControllerAnimated:YES]; }); } 
+2
source

I just made a demo project based on your situation. And I do not face any problem. Therefore, I think that there may be a problem in the way you represent the second controller.

In your button, click, try this code:

 - (IBAction)searchButtonPressed:(id)sender { UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; //idSecondVC is the storyboard id of second view controller SecondVC *SecondVC = [main instantiateViewControllerWithIdentifier:@"idSecondVC"]; [self presentViewController:SecondVC animated:YES completion:nil]; } 

And in your controller number 2, I just used the code above:

 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; cell.textLabel.text = [NSString stringWithFormat:@"Cell %ld",indexPath.row]; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self openLazyPDF]; } - (void)openLazyPDF { NSString *phrase = nil; // Document password (for unlocking most encrypted PDF files) NSArray *pdfs = [[NSBundle mainBundle] pathsForResourcesOfType:@"pdf" inDirectory:nil]; NSString *filePath = [pdfs firstObject]; assert(filePath != nil); // Path to first PDF file LazyPDFDocument *document = [LazyPDFDocument withDocumentFilePath:filePath 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__, filePath, phrase); } } #pragma mark - LazyPDFViewControllerDelegate methods - (void)dismissLazyPDFViewController:(LazyPDFViewController *)viewController { // dismiss the modal view controller [self dismissViewControllerAnimated:YES completion:NULL]; } 

And for me everything is working fine.

0
source

It sounds like you need the same macro for the present as firing. So you wrote

 #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 

You therefore need to

 #if (DEMO_VIEW_CONTROLLER_PUSH == TRUE) [self.navigationController popViewControllerAnimated:YES]; #else // presented in a modal view controller [self dismissViewControllerAnimated:YES completion:NULL]; #endif // DEMO_VIEW_CONTROLLER_PUSH 

You may have disabled the main thread, and you can always add the confirmation you want to verify, or, as suggested, use dispatch_async to be sure.

NSAssert([NSThread isMainThread)];

I prefer to assert when I know all the flows through a piece of code, because it shows my assumptions about the future to me (or another) and does not leave a code that looks like it knows something that I do not (oh, they use dispatch_async on main, so there must be some other flow magic going deeper).

0
source
 - (void)dismissLazyPDFViewController:(LazyPDFViewController *)viewController { if (![NSThread isMainThread]) { dispatch_async(dispatch_get_main_queue(), ^ { [self dismissLazyPDFViewController:viewController]; }); return; } if (viewController.navigationController) { [viewController.navigationController popViewControllerAnimated:YES]; } else { [viewController dismissViewControllerAnimated:YES completion:nil]; } } 
0
source

Source: https://habr.com/ru/post/1246011/


All Articles