I am trying to dismiss a UIPopoverViewControler from a button in the Popover. In addition I want it to transfer the data back to the main view. I have it working for a modalViewController but not for a Popover. Does anyone know how I can achieve this?
//popover
- (IBAction) save:(id)sender
{
if ([self startDateIsValid] && [self endDateIsValid])
{
[[self parentViewController] setDatesForEvent:startDate eventEndDate:endDate allDay:[allDaySwitch isOn]];
[self dismissModalViewControllerAnimated:YES];
}
}
//AddEventViewController_iPad
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "dateViewPopOverViewController_iPad.h"
@interface AddEventViewController_iPad : UIViewController <UITableViewDelegate,UITableViewDataSource, MFMailComposeViewControllerDelegate, UITextFieldDelegate, UIAlertViewDelegate,UIPopoverControllerDelegate,UINavigationControllerDelegate,UIPopoverControllerDelegate,ABPeoplePickerNavigationControllerDelegate, ABNewPersonViewControllerDelegate,DismissPopoverDelegate> {
//datePopover
#import <UIKit/UIKit.h>
#import "AddEventViewController_iPad.h"
@protocol DismissPopoverDelegate <NSObject>
- (void) dismissWithData:(NSString *)data;
@end
@interface dateViewPopOverViewController_iPad : UIViewController<UIPopoverControllerDelegate> {
In the original dialogue above "im getting an error on the line @class YourViewController : UIViewController { id delegate; } it says i need a ; – BDGapps"
The answer is very simple. It's a type. Change @class to @interface and all is well.
Sharrps answer is perfectly good, but here's a slightly different approach that may be quicker if you're presenting a subclassed view controller.
So if you've subclassed the UIViewController that's being presented, define a property on it pointing to a UIPopoverController. In your presenting view controller, instantiate your custom view controller, instantiate your popover with said custom view controller, then assign the custom view controller it's property to point to the popover controller containing it.
When it comes time to dismiss, your controller has a reference to it's popover and can dismiss it. The popover will also have a pointer to it's parent view controller, so you can perform any actions you need with regards to your model via your original presenting view controller.
Idea is simple.
YourViewController
- it's viewController ofUIPopoverController
.MainViewController
- controller where you createUIPopoverController
YourViewController
with dismiss methodid<DismissDelegateProtocol>
inYourViewController
DismissDelegateProtocol
inMainViewController
DismissDelegateProtocol
inMainViewController
YourViewController
inMainViewController
set delegate property (yourViewController.delegate = self;
)[self.delegate dismissWithData:dataToTransfer];
In code it should be like this:
In MainViewController.h:
In MainViewController.m:
In YourViewController.h:
In YourViewController.m: