lemontwist
lemontwist

Reputation: 301

Dismiss popover on UIButton click is not working, what am I doing wrong?

I am attempting to dismiss a popover as well as return a variable when a button is clicked in the popover. There are four buttons and on selection of a button, the variable (based on the button) will be returned to the original view controller and the popover will close. I am unsure of how to attempt the passing of the variable, but I am attempting to at least do the popover close on button click using this page (http://stackoverflow.com/questions/3565968/dismiss-popover-using-uibutton), which for some reason doesn't work for me. When I click on the button absolutely nothing happens.

AddWineViewController is the "root" view controller

//AddWineViewController.h
//this is the "root" view controller
#import "WineStyleViewController.h"
@interface AddWineViewController : UIViewController <UIPopoverControllerDelegate, MyPopoverDelegate>
@property (nonatomic, retain) UIPopoverController *myPopoverController;

 

//AddWineViewController.m
@implementation AddWineViewController
@synthesize myPopoverController;
-(void)didClickCancelButton {
    //I would like to have the variable passed here, something like self.wineStyle.text=wineStyle; where wineStyle is the variable from the popover.
    [myPopoverController dismissPopoverAnimated:YES];
}

WineStyleViewController is the popover view controller

//WineStyleViewController.h
@protocol MyPopoverDelegate <NSObject>
    -(void)didClickCancelButton;
@end
@interface WineStyleViewController : UIViewController
@property (nonatomic, assign) id<MyPopoverDelegate> delegate; 
@property (nonatomic, strong) NSString *wineStyle;
- (IBAction)redWineButton:(id)sender;

 

//WineStyleViewController.m
@implementation WineStyleViewController
@synthesize wineStyle;
@synthesize delegate;

- (IBAction)redWineButton:(id)sender {
    wineStyle=@"Red";
    [self.delegate didClickCancelButton];
}

Upvotes: 0

Views: 1819

Answers (5)

Amy Plant
Amy Plant

Reputation: 686

I'll make a few assumptions first, being that all of your connections in your storyboard are connected, and all objects are properly created, and that you have set delegate to self in your segue.

Just to be certain ... make sure your segue includes:

// set your destination view controller
WineStyleViewController *destinationController = segue.destinationViewController;
destinationController.delegate=self;

In your delegate (popover): WineStyleViewController, you define your protocol as:

@protocol MyPopoverDelegate <NSObject>
    -(void)didClickCancelButton;
@end

but you want to pass back the wineStyle to the root controller (AddWineViewController) so you need to 1) add the variable (wineStyle) to pass back and 2) pass along your delegate's ViewController (I took the liberty to change to Save instead of cancel)

@protocol MyPopoverDelegate <NSObject>
-(void)didClickSaveButton (WineStyleViewController *)wineStyleViewController withWineStyle (NSString *)wineStyle;
@end

Now going back to AddWineViewController:

your -(void)didClickCancelButton:(WineStyle)wineStyle is where you will receive from your delegate your wine style, so this method should look more like: (again changing it save)

-(void)didSave
{
    // your methods for dealing with your added wineStyle go hear, as
    // you will already have wineStyle sent back to you from your delegate
    [myPopoverController dismissPopoverAnimated:YES];
} 

hope that helps :D

Upvotes: 1

Amy Plant
Amy Plant

Reputation: 686

This tutorial really helped me out with a similar issue: http://ios.biomsoft.com/2011/10/17/beginning-storyboards-in-ios-5-part-2/

Upvotes: 0

Mani
Mani

Reputation: 1635

To pass "wineStyle" to your "MyPopover" protocol, simply add the variable to the protocol:

@protocol MyPopoverDelegate <NSObject>
    -(void)didClickCancelButton:(WineStyle)wineStyle;
@end

..and make sure its sent to the delegate, like this:

if ([delegate conformsToProtocol:@protocol(MyPopoverDelegate)]) {
   if ([delegate respondsToSelector:@selector(didClickCancelButton:)]) {
       [delegate didClickCancelButton:selectedWineStyle];
   }
}

Regarding the Popover not being dismissed, make sure that you store a reference to the UIPopoverController and simply call:

- (void)didClickCancelButton:(WineStyle)wineStyle {
   // ... do something with "wineStyle" here
   [currentPopover dismissPopoverAnimated:YES];
}

Upvotes: 1

Chris Trahey
Chris Trahey

Reputation: 18290

I assume you have correctly made the IBAction connection (and the method is actually executing when you press the button); so then my next thought is that the self.delegate is not being set properly as the controller is coming on-screen (in the AddWineViewController's method where the popover is presented).

Additionally, I should mention that I would probably change the interface of the protocol a little bit with a few things in mind:

  1. I would not tell the delegate that cancel was pressed to trigger the dismissal of the popover; tell it instead that a selection has been made.
  2. I prefer protocol methods which pass the "child" along in the signature: [self.delegate didChooseWineStyle: self]

Upvotes: 1

Dustin
Dustin

Reputation: 6803

You need to tell the popover controller what popover to possess, you aren't doing that. I'm not sure how you're presenting your view, but you should have something like

   UIPopoverController paparazzi = [[UIPopoverController alloc] initWithContentViewController:temp];

Upvotes: 0

Related Questions