本文介绍了如何从 Popover 中的按钮关闭 UIPopover的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 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

  1. YourViewController 中声明协议,使用dismiss 方法
  2. YourViewController中声明id类型的属性
  3. MainViewController 中声明对 DismissDelegateProtocol 的支持
  4. MainViewController中实现DismissDelegateProtocol的dismiss方法
  5. 当你在 MainViewController 中创建 YourViewController 时,设置委托属性 (yourViewController.delegate = self;)
  6. 在操作中,对触摸按钮的响应调用委托方法:[self.delegate deniedWithData:dataToTransfer];
  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:

在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 21:14