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];
}
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:
In your delegate (popover): WineStyleViewController, you define your protocol as:
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)
Now going back to AddWineViewController:
your
-(void)didClickCancelButton:(WineStyle)wineStyleis where you will receive from your delegate your wine style, so this method should look more like: (again changing it save)hope that helps 😀