我正在尝试从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;
)[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];
}