How to dismiss UIPopover from a button in the Popover

前端 未结 3 1978
花落未央
花落未央 2020-12-08 11:59

I am trying to dismiss a UIPopoverViewControler from a button in the Popover. In addition I want it to transfer the data back to the main view. I have it working for a modal

相关标签:
3条回答
  • 2020-12-08 12:26

    Sharrps answer is perfectly good, but here's a slightly different approach that may be quicker if you're presenting a subclassed view controller.

    So if you've subclassed the UIViewController that's being presented, define a property on it pointing to a UIPopoverController. In your presenting view controller, instantiate your custom view controller, instantiate your popover with said custom view controller, then assign the custom view controller it's property to point to the popover controller containing it.

    When it comes time to dismiss, your controller has a reference to it's popover and can dismiss it. The popover will also have a pointer to it's parent view controller, so you can perform any actions you need with regards to your model via your original presenting view controller.

    0 讨论(0)
  • 2020-12-08 12:36

    In the original dialogue above "im getting an error on the line @class YourViewController : UIViewController { id delegate; } it says i need a ; – BDGapps"

    The answer is very simple. It's a type. Change @class to @interface and all is well.

    @protocol DismissPopoverDelegate
    - (void) dismissPopover:(NSObject *)yourDataToTransfer;
    @end
    
    
    @interface YourViewController : UIViewController {
        id<DismissPopoverDelegate> delegate;
    }
    
    0 讨论(0)
  • 2020-12-08 12:43

    Idea is simple. YourViewController - it's viewController of UIPopoverController. MainViewController - controller where you create UIPopoverController

    1. Declare protocol in YourViewController with dismiss method
    2. Declare property of type id<DismissDelegateProtocol> in YourViewController
    3. Declare support of DismissDelegateProtocol in MainViewController
    4. Implement dismiss method of DismissDelegateProtocol in MainViewController
    5. When you create YourViewController in MainViewController set delegate property (yourViewController.delegate = self;)
    6. In action, that response to button touching call delegate method: [self.delegate dismissWithData:dataToTransfer];

    In code it should be like this:

    In MainViewController.h:

    #import "YourViewController.h"
    @class MainViewController: UIViewController < DismissPopoverDelegate >
    

    In MainViewController.m:

    - (void) dismissPopover:(NSObject *)yourDataToTransfer
    { /* Dismiss you popover here and process data */ }
    
    ...
    // Some method, when you create popover
    {
        YourViewController *vc = ... ;
        vc.delegate = self; // this delegate property should be declared as assign
    }
    

    In YourViewController.h:

    @protocol DismissPopoverDelegate
    - (void) dismissPopover:(NSObject *)yourDataToTransfer;
    @end
    
    @class YourViewController : UIViewController
    {
        id<DismissPopoverDelegate> delegate;
    }
    
    @property (nonatomic, assign) id<DismissPopoverDelegate> delegate;
    

    In YourViewController.m:

    - (void) methodWhenYouWantToDismissPopover
    {
        [self.delegate dismissPopover:data];
    }
    
    0 讨论(0)
提交回复
热议问题