单击uibutton时关闭popover

时间:2013-02-24 15:23:35

标签: xcode uipopovercontroller

我确实有一个以编程方式生成的弹出视图控制器,没有来自主视图控制器的故事板,并且一旦用户在弹出视图控制器的工具栏上使用完成按钮引入输入,就想要忽略它。我一直试图使用protrocoll来解散popover但它不起作用。我一直在培养和应用在互联网上发现的几个建议,但都给出了同样的负面结果。我监督一些事情吗?

以下是相关代码:

主视图控制器.h

#import <UIKit/UIKit.h>
#import "PopOverContentVC.h"

@interface CCViewController : UIViewController <UIPopoverControllerDelegate, PopOverContentVCDelegate> {

UIPopoverController *detailViewPopover;
}

@property (retain, nonatomic) UIPopoverController *detailViewPopover;

@end

主视图控制器.m文件

#import "PopOverContentVC.h"

@interface CCViewController ()

@end

@implementation CCViewController

@synthesize detailViewPopover;

...

- (void) dismissPopOverContentVC{

[detailViewPopover dismissPopoverAnimated:YES];
detailViewPopover = nil;
}

- (void) popOverSetUp{

PopOverContentVC *content = [[PopOverContentVC alloc] init];
detailViewPopover = [[UIPopoverController alloc] initWithContentViewController:content];
detailViewPopover.popoverContentSize = CGSizeMake(500., 150.);
detailViewPopover.delegate = self;
[detailViewPopover presentPopoverFromRect:[self coordinatesOfSegment]
                                   inView:self.spreadSheetView
                 permittedArrowDirections:UIPopoverArrowDirectionUp
                                 animated:YES];

- (void) segmentSelectedAtRow: (UISegmentedControl *)sender{

if (self.detailViewPopover) {

    [self.detailViewPopover dismissPopoverAnimated:YES];
    self.detailViewPopover = nil;
    [self popOverSetUp];

} else {

    [self popOverSetUp];
}
}

...
@end

popover视图控制器.h文件

 #import <UIKit/UIKit.h>

 @class PopOverContentVC;

 @protocol PopOverContentVCDelegate <NSObject>

 - (void)dismissPopOverContentVC;

 @end

 @interface PopOverContentVC : UIViewController

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

 - (void) doneButtonPressed:(id)sender;

 ...

@end

popover视图控制器.m文件

#import "PopOverContentVC.h"
#import "CCViewController.h"

@interface PopOverContentVC ()


@property (strong, nonatomic) UIBarButtonItem *doneButton;

@end

@implementation PopOverContentVC
@synthesize delegate;

...


- (void) viewDidLoad {

     ...

      self.doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonPressed:)];

...
}

- (void) doneButtonPressed:(id)sender {

     [self.delegate dismissPopOverContentVC];

}

我已经跟着NSLog完成了所有步骤,一切顺利,直到我按下完成按钮[self.delegate dismissPopOverContentVC];没有执行,因此我没有到主视图控制器执行dismissPopOverContentVC方法来解除popupview控制器。我做错了什么或者我错过了什么?

提前感谢您的任何提示。

1 个答案:

答案 0 :(得分:2)

我不认为您正在设置delegate对象的PopOverContentVC - 您只需设置delegate的{​​{1}}。在UIPopoverController添加

popoverSetup

创建content.delegate = self;

之后