Finding a Popover Layoff

I wanted to play with the UIPopupController, and I realized that I could not detect when my popover fired. My steps:
1. Create an example from Xcode (File → New Project → Utility Application)
2. Add to MainViewController.h UIPopoverControllerDelegate

 #import "FlipsideViewController.h" @interface MainViewController : UIViewController <FlipsideViewControllerDelegate,UIPopoverControllerDelegate> @property (strong, nonatomic) UIPopoverController *flipsidePopoverController; - (IBAction)showInfo:(id)sender; @end 
  • In MainViewController:
     - (IBAction) showInfo: (id) sender
     {
         if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
             FlipsideViewController * controller = [[FlipsideViewController alloc] initWithNibName: @ "FlipsideViewController" bundle: nil];
             controller.delegate = self;
             controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
             [self presentModalViewController: controller animated: YES];
         } else {
             if (! self.flipsidePopoverController) {
                 FlipsideViewController * controller = [[FlipsideViewController alloc] initWithNibName: @ "FlipsideViewController" bundle: nil];
                 controller.delegate = self;

                 self.flipsidePopoverController.delegate = self

                 self.flipsidePopoverController = [[UIPopoverController alloc] initWithContentViewController: controller];
             }
             if ([self.flipsidePopoverController isPopoverVisible]) {
                 [self.flipsidePopoverController dismissPopoverAnimated: YES];
             } else {
                 [self.flipsidePopoverController presentPopoverFromBarButtonItem: sender permittedArrowDirections: UIPopoverArrowDirectionAny animated: YES];
             }
         }
     }

     - (void) popoverControllerDidDismissPopover: (UIPopoverController *) popoverController
     {
         NSLog (@ "OLOLO");
     }

But when I click somewhere and the popover disappears, the NSLog message does not appear in the console. What am I doing wrong?

+6
source share
4 answers

set popover delegates for yourself, and also you can use two popover delegates ie: -

  /* Called on the delegate when the popover controller will dismiss the popover. Return NO to prevent the dismissal of the view. */ - (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController; /* Called on the delegate when the user has taken action to dismiss the popover. This is not called when -dismissPopoverAnimated: is called directly. */ - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController; 
+15
source

Earlier answers suggested using UIPopoverControllerDelegate, which seems to be the only reasonable way to achieve the goal. I thought it would be nice to add a practical example, since this is not the easiest thing to untie your head. My requirement was simple - I wanted the background view to be blurry and the popover to be visible, so here are the steps:

  • Connect your popover in the storyboard, adjust its content size in the attributes of the destination view manager.

  • Make your UIPopoverControllerDelegate source view controller by opening the .h file and doing something like this:

     @interface MyController : UIViewController <UIPopoverControllerDelegate> 
  • Override prepareForSegue, assign the controller of the source view as the delegate for popover, and then set the alpha value to 0.5 just before sending the segue:

     - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { UIStoryboardPopoverSegue* popover = (UIStoryboardPopoverSegue*)segue; popover.popoverController.delegate = self; self.view.alpha = 0.5; } 
  • Implement the delegation method popoverControllerDidDismissPopover. Set alpha back to 1.0 and unassign yourself as a delegate to make sure that we don't stop the ARC doing this work:

     -(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController { self.view.alpha = 1.0; popoverController.delegate = nil; } 
+4
source

Have you installed an instance of MainViewController as a popover delegate?

(if you create a popover via code) popover.delegate = self;

+2
source
 - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { UIStoryboardPopoverSegue *popoverSegue; popoverSegue = (UIStoryboardPopoverSegue *)segue; popoverController = popoverSegue.popoverController; pCVisible = YES; [[segue destinationViewController] setDelegate:self]; } - (void) setDataFromPopover { if (pCVisible) { [popoverController dismissPopoverAnimated:YES]; // THIS IS KEY! this is where the popover is dismissed, not in the popover itself } } 

AND

 //TableViewController.m - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath { //variable = whatever OtherViewController *initialView; initialView=(OtherViewController *)self.delegate; initialView.theLabel.text = variable; [initialView setDataFromPopover]; } 
0
source

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


All Articles