xcode 如何从 Popover 中的按钮关闭 UIPopover

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5922254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:58:45  来源:igfitidea点击:

How to dismiss UIPopover from a button in the Popover

objective-cxcodeiosipaduipopovercontroller

提问by BDGapps

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 中的按钮关闭 UIPopoverViewControler。此外,我希望它将数据传输回主视图。我让它适用于 modalViewController 但不适用于 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

//添加EventViewController_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> {

回答by 5hrp

Idea is simple. YourViewController- it's viewController of UIPopoverController. MainViewController- controller where you create UIPopoverController

想法很简单。YourViewController- 它是UIPopoverController. MainViewController- 您创建的控制器UIPopoverController

  1. Declare protocol in YourViewControllerwith dismiss method
  2. Declare property of type id<DismissDelegateProtocol>in YourViewController
  3. Declare support of DismissDelegateProtocolin MainViewController
  4. Implement dismiss method of DismissDelegateProtocolin MainViewController
  5. When you create YourViewControllerin MainViewControllerset delegate property (yourViewController.delegate = self;)
  6. In action, that response to button touching call delegate method: [self.delegate dismissWithData:dataToTransfer];
  1. YourViewController使用dismiss方法声明协议
  2. 声明类型的财产id<DismissDelegateProtocol>YourViewController
  3. 声明支持DismissDelegateProtocolinMainViewController
  4. 实现DismissDelegateProtocolin的dismiss方法MainViewController
  5. 当您YourViewControllerMainViewControllerset 委托属性 ( yourViewController.delegate = self;) 中创建时
  6. 实际上,对按钮触摸调用委托方法的响应: [self.delegate dismissWithData:dataToTransfer];

In code it should be like this:

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

In MainViewController.h:

在 MainViewController.h 中

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

In MainViewController.m:

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

In YourViewController.h:

在 YourViewController.h 中

@protocol DismissPopoverDelegate
- (void) dismissPopover:(NSObject *)yourDataToTransfer;
@end

@class YourViewController : UIViewController
{
    id<DismissPopoverDelegate> delegate;
}

@property (nonatomic, assign) id<DismissPopoverDelegate> delegate;

In YourViewController.m:

在 YourViewController.m 中

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

回答by isaac

Sharrps answer is perfectly good, but here's a slightly different approach that may be quicker if you're presenting a subclassed view controller.

Sharps 的回答非常好,但是如果您要呈现子类视图控制器,这里有一种稍微不同的方法可能会更快。

So if you've subclassed the UIViewController that's being presented, define a property on it pointing to a UIPopoverController. In your presenting view controller, instantiate your custom view controller, instantiate your popover with said custom view controller, then assign the custom view controller it's property to point to the popover controller containing it.

因此,如果您已经对正在呈现的 UIViewController 进行子类化,请在其上定义一个指向 UIPopoverController 的属性。在您的呈现视图控制器中,实例化您的自定义视图控制器,使用所述自定义视图控制器实例化您的弹出框,然后将自定义视图控制器的属性分配给包含它的弹出框控制器。

When it comes time to dismiss, your controller has a reference to it's popover and can dismiss it. The popover will also have a pointer to it's parent view controller, so you can perform any actions you need with regards to your model via your original presenting view controller.

当需要关闭时,您的控制器会引用它的弹出框并可以关闭它。弹出窗口还将有一个指向它的父视图控制器的指针,因此您可以通过原始呈现视图控制器对模型执行所需的任何操作。

回答by Jim Holland

In the original dialogue above "im getting an error on the line @class YourViewController : UIViewController { id delegate; } it says i need a ; – BDGapps"

在上面的原始对话中,“我在@class YourViewController : UIViewController { id delegate; } 行中出现错误,它说我需要一个; – BDGapps”

The answer is very simple. It's a type. Change @class to @interface and all is well.

答案很简单。是一种。将@class 更改为@interface,一切都很好。

@protocol DismissPopoverDelegate
- (void) dismissPopover:(NSObject *)yourDataToTransfer;
@end


@interface YourViewController : UIViewController {
    id<DismissPopoverDelegate> delegate;
}