emil.c
emil.c

Reputation: 2018

Dismiss UIPopoverController from a button within its UIViewController

This is firstPopoverViewController.h code:

#import <UIKit/UIKit.h>

@interface firstPopoverViewController : UIViewController

@end

This is my firstPopoverViewController.m code:

#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

Then there's 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

And finally 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

It's pretty basic code that displays a button and when I click this button it shows popover. I want to close this popover using button that's inside firstPopoverViewControll.m file. There's a closePop{} method, what should I put inside it to close this popover? Thanks.

By the way I'm beginner as you can see, I researched stackoverflow and there are some solutions with delegates, which seems to be working for others, but didn't work for me, could you please show me a solution on my code that I posted? Thank you people very much.

Upvotes: 0

Views: 1456

Answers (2)

RyanG
RyanG

Reputation: 4503

A delegate is the way to go. I do admit though that I was confused by them at first, but they are quite simple to setup.

In your firstPopoverController.h put this:

#import <UIKit/UIKit.h>

@protocol FirstPopoverDelegate
- (void) closedPopover;
@end

@interface firstPopoverViewController : UIViewController

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

@end

Then in your .m of you popover,

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

In your main UIViewController's .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

Then in the .m, first register to listen of the delegate by adding this to your openPop method:

this is important and easy to forget.. nothing will happen if it is not set

_firstPopoverViewController.delegate = self;

Finally, in your .m add the delegate method:

- (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];
}

Upvotes: 2

Aron C
Aron C

Reputation: 838

There may be a simpler method that I'm not aware of, but the following method should work:

Use NSNotificationCenter to post a notification back to the ViewController containing the UIPopOverController to tell it to dismiss the popover.

First, in ViewController.m viewDidLoad add:

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

Then add the following method to ViewController.m:

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

Then in irstPopoverViewController.m:

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

That should do the trick.

Upvotes: 2

Related Questions