问题描述
我试图从Popover中的按钮中解除UIPopoverViewControler。另外我希望它将数据传输回主视图。我有一个modalViewController但不适用于Popover。有谁知道我怎么做到这一点?
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 modalViewController but not for a Popover. Does anyone know how I can achieve this?
// popover
//popover
- (IBAction) save:(id)sender
{
if ([self startDateIsValid] && [self endDateIsValid])
{
[[self parentViewController] setDatesForEvent:startDate eventEndDate:endDate allDay:[allDaySwitch isOn]];
[self dismissModalViewControllerAnimated:YES];
}
}
// AddEventViewController_iPad
//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
//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
Idea is simple. YourViewController
- it's viewController of UIPopoverController
. MainViewController
- controller where you create UIPopoverController
- 使用dismiss方法在
YourViewController
中声明协议 - 声明类型
id< DismissDelegateProtocol>的属性
inYourViewController
- 声明支持
DismissDelegateProtocol
在MainViewController
- 在 DismissDelegateProtocol 的解除方法> MainViewController
- 在
MainViewController中创建
设置委托属性(YourViewController
yourViewController.delegate = self;
) - 在操作中,对按钮触摸调用委托方法的响应:
[self.delegate dismissWithData:dataToTransfer];
- Declare protocol in
YourViewController
with dismiss method - Declare property of type
id<DismissDelegateProtocol>
inYourViewController
- Declare support of
DismissDelegateProtocol
inMainViewController
- Implement dismiss method of
DismissDelegateProtocol
inMainViewController
- When you create
YourViewController
inMainViewController
set delegate property (yourViewController.delegate = self;
) - In action, that response to button touching call delegate method:
[self.delegate dismissWithData:dataToTransfer];
在代码中它应该像这个:
In code it should be like this:
在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];
}
这篇关于如何从Popover中的按钮关闭UIPopover的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!