How to setup Popover views to dismiss properly

前端 未结 1 1732
悲&欢浪女
悲&欢浪女 2021-02-09 07:55

After some work I\'ve got a popover view displaying a custom uiview inside of it appearing properly. When you tap outside of the popover view it dismisses as expected, however

1条回答
  •  日久生厌
    2021-02-09 08:26

    To dismiss the popover manually, you'll need to keep a reference to it. A good place would be in the view controller that shows the popover.

    To have the button inside the content view controller tell the original view controller (that presented the popover) to dismiss the popover, two of the possible ways are delegate+protocol or NSNotificationCenter.

    The example code below uses NSNotificationCenter.

    In the .h of the view controller that presents the popover ("PresenterViewController"), add a property for the popoverViewController:

    @interface PresenterViewController : UIViewController {
        ...
        UIPopoverController *popoverController;
    }    
    ...
    @property (nonatomic, retain) UIPopoverController *popoverController;
    @end
    

    In the .m of PresenterViewController:

    @synthesize popoverController;
    ...
    - (void)showPopover
    {
        if (self.popoverController == nil)
        {
            ContentFooViewController *content = 
                [[ContentFooViewController alloc] init];
            UIPopoverController *popover = [[UIPopoverController alloc] 
                initWithContentViewController:content];
            [content release];
            self.popoverController = popover;
            [popover release];
    
            // Listen for the "done" notification which will be posted
            // by the button in the content view controller.
            // When the notification is received, 
            // call the contentFooViewControllerDone: method...
            [[NSNotificationCenter defaultCenter] 
                addObserver:self
                selector:@selector(contentFooViewControllerDone:)
                name:@"contentFooViewControllerDone" 
                object:popoverController.contentViewController];
        }
    
        [self.popoverController presentPopoverFromRect:...
          // OR
        [self.popoverController presentPopoverFromBarButtonItem:...
    }
    
    - (void)contentFooViewControllerDone:(NSNotification *)notification
    {
        // Button in content view controller was tapped, dismiss popover...
        [self.popoverController dismissPopoverAnimated:YES];
    }
    
    - (void)dealloc 
    {
        //stop listening for notifications and release popoverController...
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        [popoverController release];
        [super dealloc];
    }
    

    In the content view controller, implement the button tap method as follows:

    - (IBAction)dismissButtonTapped
    {
        [[NSNotificationCenter defaultCenter] 
            postNotificationName:@"contentFooViewControllerDone" object:self];
    }
    

    0 讨论(0)
提交回复
热议问题