我正在尝试从Popover中的按钮关闭UIPopoverViewControler。另外,我希望它将数据传输回主 View 。我有它为modalViewController工作,但没有为Popover工作。有谁知道我怎么能做到这一点?

//弹出框

- (IBAction) save:(id)sender
{
    if ([self startDateIsValid] && [self endDateIsValid])
    {

        [[self parentViewController] setDatesForEvent:startDate eventEndDate:endDate allDay:[allDaySwitch isOn]];
        [self dismissModalViewControllerAnimated:YES];

    }

}

//AddEventViewController_iPad
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "dateViewPopOverViewController_iPad.h"
@interface AddEventViewController_iPad : UIViewController <UITableViewDelegate,UITableViewDataSource, MFMailComposeViewControllerDelegate, UITextFieldDelegate,  UIAlertViewDelegate,UIPopoverControllerDelegate,UINavigationControllerDelegate,UIPopoverControllerDelegate,ABPeoplePickerNavigationControllerDelegate, ABNewPersonViewControllerDelegate,DismissPopoverDelegate> {

//datePopover
#import <UIKit/UIKit.h>
#import "AddEventViewController_iPad.h"
@protocol DismissPopoverDelegate <NSObject>

- (void) dismissWithData:(NSString *)data;

@end

@interface dateViewPopOverViewController_iPad : UIViewController<UIPopoverControllerDelegate> {

最佳答案

想法很简单。 YourViewController-它是UIPopoverController的viewController。 MainViewController-在其中创建UIPopoverController的 Controller

  • 使用关闭方法
  • 声明YourViewController中的协议(protocol)
  • id<DismissDelegateProtocol>中声明YourViewController类型的属性
  • DismissDelegateProtocol中声明对MainViewController的支持
  • DismissDelegateProtocol中实现MainViewController的关闭方法
  • YourViewController中创建MainViewController时,请设置委托(delegate)属性(yourViewController.delegate = self;)
  • 实际上,对触摸按钮的调用委托(delegate)方法的响应:[self.delegate dismissWithData:dataToTransfer];

  • 在代码中应该是这样的:

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

    在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
    }
    

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

    在YourViewController.m中:
    - (void) methodWhenYouWantToDismissPopover
    {
        [self.delegate dismissPopover:data];
    }
    

    09-30 21:23