This has me scratching my head. I have a view controller that implements UIPopoverControllerDelegate:
#interface MyViewController : UIViewController<UIPopoverControllerDelegate> {
It implements the methods in that protocol, like so:
- (bool)popoverControllerShouldDismissPopover
{
NSLog(#"THIS IS NEVER CALLED");
return true;
}
- (void)popoverControllerDidDismissPopover
{
NSLog(#"THIS IS ALSO NEVER CALLED");
}
I create the view controller to be shown in a popover, and the popover controller, like so:
-(IBAction)buttonPress:(id)sender
{
self.popoverViewController = [[MyPopoverViewController alloc] init];
self.popover = [[UIPopoverController alloc] initWithContentViewController:popoverViewController];
self.popover.delegate = self;
CGRect frame = button.frame;
[self.popover presentPopoverFromRect:frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
I'm very clearly setting popover.delegate = self. HOWEVER, the popoverViewController delegate methods I've implemented are never being called.
Any ideas?
Edit: Note, I'm using ARC & LLVM.
I think your delegate method implementations are incorrect. Try
- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController
rather than
- (bool)popoverControllerShouldDismissPopover
and
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
rather than
- (void)popoverControllerDidDismissPopover
it's important that you get the method names and parameters correct, otherwise they will not be called.
Good luck!
Related
Really simple question i think.
i have a main view and a popover view.
i am trying to make a custom delegate which will enable me to close the popover view at certain times. My code is posted below. The real simple issue i am having is my code dosnt appear to be entering the delegate code. Any ideas as to why? It builds and runs but nothing appears to happen, i have put NSLog statments, the popover nslog appears but the function in
mainview dismissPopover does nothing.
Mainview.h
#interface MainScreen : UIViewController<DismissPopoverDelegate>
Mainview.m
- (void) dismissPopover:(NSNumber *)dataa
{ /* Dismiss you popover here and process data */
[popoverController dismissPopoverAnimated:YES];
NSLog(#"OLOLO");
}
Popover.h
#protocol DismissPopoverDelegate
- (void) dismissPopover:(NSNumber *)yourDataToTransfer;
#end
#interface SelectAgePopOver : UIViewController<UITableViewDataSource,
UITableViewDelegate,UIPopoverControllerDelegate>{
NSArray *items;
id<DismissPopoverDelegate> delegate;
}
#property (nonatomic, assign) id<DismissPopoverDelegate> delegate;
Popover.m
[self.delegate dismissPopover:selrow];
where i want the delegate called.
Thanks
the popover view is called by the following method in main view.m
controller = [[SelectAgePopOver alloc] initWithNibName:#"SelectAgePopOver" bundle:nil];
popoverController = [[UIPopoverController alloc] initWithContentViewController:controller];
[popoverController setDelegate:self];
popoverController.popoverContentSize = CGSizeMake(250, 294);
if ([popoverController isPopoverVisible]) {
[popoverController dismissPopoverAnimated:YES];
} else {
CGRect popRect = CGRectMake((self.AgeRangeTextField.frame.origin.x+50),
(self.AgeRangeTextField.frame.origin.y+50),
(self.AgeRangeTextField.frame.size.width),
(self.AgeRangeTextField.frame.size.height));
[popoverController presentPopoverFromRect:popRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
On where you instantiate the instance of the SelectAgePopOver, you need to set the delegate of the SelectAgePopOver instance to the MainScreen (self) in the Mainview.m, for example:
controller = [[SelectAgePopOver alloc] initWithNibName:#"SelectAgePopOver" bundle:nil];
;
controller.delegate = self;
Please try to set the controller delegate as shown above.
In the SelectAgePopOver.h class file, you do not need to set UIPopOverControllerDelegate though.
In my application I have a presentViewController, and inside it I have a button that opens a popover. In this popover I have a barButtonItem to save de data of this popover. I would like that when the user taps outside of the popover, the data could be saved too.
I've tried to use the popoverControllerDidDismissPopover method in the presentViewController view. I have the delegate but when I tap outside of the popover this method is not called.
What can I do?
Thanks!!
Add this line of code while adding popOver:
popover.delegate = self;
Also register popOverDelegate in .h file where u present your popOver COntroller
#interface yourViewController : UIViewController<UIPopoverControllerDelegate>
Please, pay attention to docs!
"Called on the delegate when the user has taken action to dismiss the popover.
This is not called when the popover is dismissed programmatically."
It was my case because my popover was closing on button tap with this method:
dismiss(animated: true, completion: nil)
You probably already solved it, but I just faced the same problem. I'm holding a instance of UIPopoverController in my Viewcontroller and had it this way:
self.popover.delegate = self;
self.popover = [[UIPopoverController alloc] initWithContentViewController:wgtvc];
of course this doesn't work because I'm initializing the UIPopoverController AFTER setting the delegate, which overrides the delegate setting. So the correct way is to FIRST initialize the UIPopovercontroller and THEN setting the delegate
self.popover = [[UIPopoverController alloc] initWithContentViewController:wgtvc];
self.popover.delegate = self;
Maybe you are reinitializing your UIPopoverController somewhere - just set the delegate again after reinitializing.
I had the same problem and I solved it by handling it different for iOS8.
Presentation code
UIViewController *searchViewController = [[UIViewController alloc] init];
[[searchViewController view] addSubview:_searchOptions];
[searchViewController setModalPresentationStyle:UIModalPresentationPopover];
[searchViewController setPreferredContentSize:CGSizeMake(500, 400)];
[_searchOptions setHidden:NO];
[_searchOptions setFrame:[[searchViewController view] bounds]];
[_searchOptions setAutoresizingMask:UIViewAutoresizingFlexibleWidthAndHeight];
if (CRIdiomToolsIsIOS8OrHigher())
{
UIPopoverPresentationController *popOverPresentationController = [searchViewController popoverPresentationController];
[popOverPresentationController setDelegate:self];
[popOverPresentationController setSourceView:[_searchOptionsButton disclosureView]];
[popOverPresentationController setSourceRect:[[_searchOptionsButton disclosureView] bounds]];
[self presentViewController:searchViewController animated:YES completion:nil];
}
else
{
UIPopoverController *popOverControler = [[UIPopoverController alloc] initWithContentViewController:searchViewController];
[popOverControler setDelegate:self];
[popOverControler setPopoverContentSize:CGSizeMake(500, 400)];
[popOverControler presentPopoverFromRect:[[_searchOptionsButton disclosureView] bounds] inView:[_searchOptionsButton disclosureView] permittedArrowDirections:UIPopoverArrowDirectionUp|UIPopoverArrowDirectionLeft animated:YES];
}
Delegate calls
#pragma mark Delegate Methods: UIPopoverControllerDelegate
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
[self showSearchOptions:NO animated:YES];
}
#pragma mark Delegate Methods: UIPopoverPresentationControllerDelegate
- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController
{
[self showSearchOptions:NO animated:YES];
}
I had the same issue. You will need to retain the popover object, that way the delegate method gets called. Its strange but it works.
#property (nonatomic, retain) UIPopoverController *popupObject;
UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:viewController];
popup.delegate = self;
[popup presentPopoverFromRect:presentationRect inView:self permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
self.popupObject = popup; //Retained
-(void) popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
//Do whatever operation you need to perform
self.popupObject = nil;
}
I know this questions is old but hopefully it helps someone out there...
The issue is in the initialization of your popover controller. If you have established the popover segue in the storyboard you need to have a reference to this popover in order for the delegate to be called when it is dismissed.
In your prepare for segue method:
Instead of:
self.popoverController = [[UIPopoverController alloc]initWithContentViewController:segue.destinationViewController];
self.popoverController.delegate = self;
You need:
self.popoverController = [(UIStoryboardPopoverSegue *)segue popoverController];
self.popoverController.delegate = self;
Then make sure to correctly handle if the when the popover should appear in
(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
I have two views setup ( inside a TabBar). The DetailView with a button that calls a PopOver with a NavigationController+UITableView (RootView) loading data from CoreData. I have a problem passing data from the UITableView to the DetailView. I have a protocol declared in RootView and used in the DetailView.
Here is the code I use to create the PopOver from the button because I think I have some delegate issues. Any help will be amazing,
- (IBAction)zoneListButtonController
{
if (self.controladorPopOver == nil) {
ipadrootviewController = [[iPadRootViewController alloc] initWithNibName:#"iPadRootView" bundle:[NSBundle mainBundle]];
UINavigationController *ipadnavController = [[UINavigationController alloc]
initWithRootViewController:ipadrootviewController];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:ipadnavController];
self.controladorPopOver = popover;
popover.delegate = self;
self.title = #"Countries";
popover.popoverContentSize = CGSizeMake(320, 300);
[self.controladorPopOver presentPopoverFromRect:CGRectMake(112, 20, 86, 27) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[ipadnavController release];
[controladorPopOver release];
}
}
An instance of the SubZone1iPadController doesn't exist when you create the popover in DetailView so you can't set its delegate property directly from the DetailView.
One option is to also add the delegate property to the iPadRootViewController which you can set in the zoneListButtonController method. Then, when ipadrootviewController creates the SubZone1iPadController, pass along the delegate.
So in both ipadrootviewController and SubZone1iPadController, add a delegate property:
#property (nonatomic,assign) id <SubZone1Tap> delegate;
Then, in the zoneListButtonController method, set the delegate property on iPadRootViewController:
ipadrootviewController = [[iPadRootViewController alloc] init...
ipadrootviewController.delegate = self;
Then, where ipadrootviewController creates SubZone1iPadController:
SubZone1iPadController *sz1 = [[SubZone1iPadController alloc] init...
sz1.delegate = self.delegate;
[self.navigationController pushViewController:...
[sz1 release];
Finally, in the DetailView, make sure the delegate method is implemented. For example:
-(void)SubZone1Tap:(NSString *)name
{
NSLog(#"SubZone1Tap, name = %#", name);
//dismiss the popover if that's what you need to do...
[controladorPopOver dismissPopoverAnimated:YES];
}
I'm doing an alloc/init with my UIPopoverController, then release it in the delegate method. Whenever I perform a "build and analyze", I get memory warnings with "potential leaks" - am I doing something wrong or is there a way to get rid of those warnings?
Thanks a lot!
- (void) somewhere {
MyViewController *vc = [[MyViewController alloc] init];
UIPopoverController *popover=[[UIPopoverController alloc] initWithContentViewController:vc];
[vc release];
// show the popover
[popover presentPopoverFromRect:[cell frame] inView:self.tableView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
popover.delegate = self;
}
- (void) popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
[popoverController release];
popoverController = nil;
}
EDIT: show complete somewhere-function
First of all, in the popoverControllerDidDismissPopover: method you should not release the popoverController instance, since it will be released by the framework.
Second, what's the meaning of that somewhere method? You should post the complete implementation here ... I suppose you are showing up the popover view, so you should do something like this:
- (void) somewhere {
// I suppose you have a local variable to hold the controller
_myPopover = [[UIPopoverController alloc] initWithContentViewController:vc];
_myPopover .delegate = self;
// ... do other things to configure the popover, if necessary
// I suppose you show it, with something like this
[_myPopover presentPopoverFromRect:yourRect inView:yourView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
Then, when you no longer need the popover controller, you can release it. You could try with your code, this way:
- (void) popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
[_myPopover release];
_myPopover = nil;
}
This way you will always hold the instance of the popover where you need it, and release it when you have finished with it. In your previous implementation that instance was not bound to anything.
I used a Popover to display image in it. When the user touch a button, the popover appears with a slideshow inside.
I initialize the Popover like this : `
- (IBAction)showPopover:(UIButton *)sender {
myPopover *content = [[myPopover alloc] init];
detailViewPopover = [[UIPopoverController alloc] initWithContentViewController:content];
detailViewPopover.popoverContentSize = CGSizeMake(600., 400.);
detailViewPopover.delegate = self;
[detailViewPopover presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[content release];
}
`
detailViewPopover is a UIPopoverController, I declare it my .h.
I dismiss the Popover like this : `
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
// If a popover is dismissed, set the last button tapped to nil.
[popoverController release];
}`
When I run my app, it works until I got "-[UIPopoverController release]: message sent to deallocated instance 0x1b29b0" and my apps crashes...
I understand I release too much time my UIPopoverController, but I don't know where. Is my implementation good ?
Thanks for your help
Let me know if you need more information, I will edit the post
You shouldn't release your popoverController here.
You need to call release on detailViewPopover in your current view controllers dealloc method
- (void) dealloc
{
[detailViewPopover release];
[super dealloc];
}