从UIViewController中的按钮中解除UIPopoverController

时间:2013-11-21 16:38:50

标签: ios objective-c uiviewcontroller

这是firstPopoverViewController.h代码:

#import <UIKit/UIKit.h>

@interface firstPopoverViewController : UIViewController

@end

这是我的firstPopoverViewController.m代码:

#import "firstPopoverViewController.h"

@interface firstPopoverViewController ()

@end

@implementation firstPopoverViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.contentSizeForViewInPopover = CGSizeMake(300, 290);

    // Header label
    UILabel *h1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 85)];
    h1.font = [UIFont fontWithName:@"myFont" size:22.0];
    h1.textColor = [UIColor blackColor];
    h1.textAlignment = NSTextAlignmentCenter;
    h1.text = @"Heading";
    h1.numberOfLines = 0;
    h1.backgroundColor = [UIColor greenColor];

    // Ok button BG View
    UIView *buttonBG = [[UIView alloc] initWithFrame:CGRectMake(0, 300-75, 300, 75)];
    buttonBG.backgroundColor = [UIColor greenColor];

    // Ok button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(300/2-130/2, 290-35-15, 130, 35);
    button.backgroundColor = [UIColor whiteColor];
    [button setTitle:@"OK" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(closePop) forControlEvents:UIControlEventTouchUpInside];
    button.adjustsImageWhenHighlighted=YES;

    // Adding views
    [self.view addSubview:h1];
    [self.view addSubview:buttonBG];
    [self.view addSubview:button];

}

-(void)closePop {

}

@end

然后是ViewController.h:

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

@interface ViewController : UIViewController

@property (strong, nonatomic) UIButton *popButton;
@property (strong, nonatomic) firstPopoverViewController *firstPopoverViewController;
@property (strong, nonatomic) UIPopoverController *firstPopover;

@end

最后是ViewController.m:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    /* ##### UIPopController stuff ##### */
    UIImage *popButtonImage = [UIImage imageNamed:@"menu.png"];
    _popButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _popButton.frame = CGRectMake(0, 0, 73, 66);
    [_popButton addTarget:self action:@selector(openPop) forControlEvents:UIControlEventTouchUpInside];
    _popButton.adjustsImageWhenHighlighted=NO;
    [_popButton setImage:popButtonImage forState:UIControlStateNormal];

    [self.view addSubview:_popButton];

}

-(void)openPop {

    if (_firstPopoverViewController == nil) {
        //Create the _firstPopoverViewController.
        _firstPopoverViewController = [[firstPopoverViewController alloc] init];
    }

    if (_firstPopover == nil) {
        _firstPopover = [[UIPopoverController alloc] initWithContentViewController:_firstPopoverViewController];
        _firstPopover.popoverContentSize = CGSizeMake(300, 290);
        [_firstPopover presentPopoverFromRect:CGRectMake(0,0, 73, 66) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
        NSLog(@"show");
    } else {
        NSLog(@"dismiss");
        [_firstPopover dismissPopoverAnimated:YES];
        _firstPopover = nil;
    }
}

@end

这是显示按钮的非常基本的代码,当我点击此按钮时,它显示弹出窗口。我想使用firstPopoverViewControll.m文件中的按钮关闭此弹出窗口。有一个closePop {}方法,我应该把它放在里面来关闭这个popover?感谢。

顺便说一下,我是初学者,你可以看到,我研究了stackoverflow,并且有一些代表的解决方案,这似乎适用于其他人,但对我不起作用,请你帮我看一下我发布的代码?非常感谢你们。

2 个答案:

答案 0 :(得分:2)

可能有一个我不知道的更简单的方法,但以下方法应该有效:

使用NSNotificationCenter将通知发回包含UIPopOverController的ViewController,告诉它解除弹出窗口。

首先,在ViewController.m viewDidLoad中添加:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closePop:) name:@"ClosePopOver" object:nil];

然后将以下方法添加到ViewController.m:

- (void)closePop:(NSNotification *)notification {
      [_firstPopover dismissPopoverAnimated:YES];
}

然后在irstPopoverViewController.m中:

- (void)closePop {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ClosePopOver" object:nil];        
}

这应该可以解决问题。

答案 1 :(得分:2)

代表是要走的路。我承认,虽然我最初对它感到困惑,但它们设置起来非常简单。

在你的firstPopoverController.h中输入:

#import <UIKit/UIKit.h>

@protocol FirstPopoverDelegate
- (void) closedPopover;
@end

@interface firstPopoverViewController : UIViewController

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

@end

然后在你的.m你的popover,

-(void)closePop 
{
     [self.delegate closedPopover];
}

在你的主要UIViewController的.h:

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

@interface ViewController : UIViewController <FirstPopoverDelegate>

@property (strong, nonatomic) UIButton *popButton;
@property (strong, nonatomic) firstPopoverViewController *firstPopoverViewController;
@property (strong, nonatomic) UIPopoverController *firstPopover;

@end

然后在.m中,首先注册以通过将其添加到您的openPop方法来收听代表:

这很重要且容易忘记..如果没有设置就不会发生

_firstPopoverViewController.delegate = self;

最后,在.m中添加委托方法:

- (void)closedPopover
{
    //you can also pass data back in this function, just modify its parameters here and when you define it in the .h of the popover 
    [_firstPopoverViewController dismissPopoverAnimated:YES];
}