问题描述
我正在尝试从 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?
//弹出框
- (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
- 在
YourViewController
中声明协议,使用dismiss 方法 - 在
YourViewController
中声明id
类型的属性 - 在
MainViewController
中声明对DismissDelegateProtocol
的支持 - 在
MainViewController
中实现DismissDelegateProtocol
的dismiss方法 - 当你在
MainViewController
中创建YourViewController
时,设置委托属性 (yourViewController.delegate = self;
) - 在操作中,对触摸按钮的响应调用委托方法:
[self.delegate deniedWithData: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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!