I can't set data inside the viewController "searchController" managed by a UIPopoverController:
in viewDidLoad i have:
searchController = [[PopOverSearchController alloc] initWithNibName:#"PopOverSearchController" bundle:nil];
popoverController = [[UIPopoverController alloc] initWithContentViewController:searchController];
I do a method for call this popover that work fine:
if ([popoverController isPopoverVisible]) {
[popoverController dismissPopoverAnimated:YES];
} else {
searchController.data = data;
[searchController.tableResult reloadData];
CGRect popRect = CGRectMake(self.searchBar.frame.origin.x,
self.searchBar.frame.origin.y +15,
self.searchBar.frame.size.width,
self.searchBar.frame.size.height);
[popoverController presentPopoverFromRect:popRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
[searchController.tableResult reloadData]; was not called: the popup appear (above the search bar )but with an empty table
Related
I use UINavigationController inside UIPopoverController
-(void)showEditMenuFrom:(UIButton *)button{
if (self.popover) {
[self.popover dismissPopoverAnimated:YES];
self.popover = nil;
}
else {
EditMenuViewController *editMenuViewController = [[EditMenuViewController alloc] initWithNibName:#"EditMenuViewController" bundle:nil];
UINavigationController *actionsNavigationController = [[UINavigationController alloc] initWithRootViewController:editMenuViewController];
actionsNavigationController.delegate = self;
// switch for iPhone and iPad.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
self.popover = [[UIPopoverController alloc] initWithContentViewController:actionsNavigationController];
self.popover.delegate = self;
// CGRect presentFrame = CGRectMake(button.frame.origin.x-43, button.frame.origin.y-10, button.frame.size.width, button.frame.size.height);
[self.popover presentPopoverFromRect:button.frame inView:button permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
[self presentViewController:actionsNavigationController animated:YES completion:^{
NSLog(#"Activity complete");
}];
}
}
}
And I try to resize view inside UIPopover when navigate and these are result:
Root view:
2.Push to other VC
3.Pop back:
You can see the height of root VC can't change back to origin size.
I try to set contentSizeForViewInPopover in viewDidAppear, viewDidLoad and in UINavigationControllerDelegate but no methods work
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
NSLog(#"Show VIEW %#",viewController);
if ([viewController isKindOfClass:[EditMenuViewController class]]) {
viewController.contentSizeForViewInPopover = CGSizeMake(160.0, 160.0);
} else {
viewController.contentSizeForViewInPopover = CGSizeMake(320.0, 320.0);
}
}
How to solve this problem? Thanks!
I end up my question for someone meet this solution:
First, you need to declare a #property inside the VC which pop back on stack
#property (nonatomic, strong) UIPopoverController *popover;
Then assign it to UIPopoverController in main code
self.popover = [[UIPopoverController alloc] initWithContentViewController:actionsNavigationController];
self.popover.delegate = self;
editMenuViewController.popover = self.popover;
And Finally, setting the size in viewWillAppear of VC you want to resize
-(void)viewWillAppear:(BOOL)animated
{
[self.popover setPopoverContentSize:CGSizeMake(160,160)];
self.contentSizeForViewInPopover = CGSizeMake(160,160);
[super viewWillAppear:animated];
}
Try with bellow example miht be usefull:-
-(void)showEditMenuFrom:(UIButton *)button{
if (self.popover) {
[self.popover dismissPopoverAnimated:YES];
self.popover = nil;
}
else {
EditMenuViewController *editMenuViewController = [[EditMenuViewController alloc] initWithNibName:#"EditMenuViewController" bundle:nil];
UINavigationController *actionsNavigationController = [[UINavigationController alloc] initWithRootViewController:editMenuViewController];
actionsNavigationController.delegate = self;
// switch for iPhone and iPad.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
self.popover = [[UIPopoverController alloc] initWithContentViewController:actionsNavigationController];
self.popover.delegate = self;
popover.popoverContentSize =CGSizeMake(250,200); //Addint this line for setContentsize
[self.popover presentPopoverFromRect:button.frame inView:button permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
[self presentViewController:actionsNavigationController animated:YES completion:^{
NSLog(#"Activity complete");
}];
}
}
}
In EditMenuViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//self.clearsSelectionOnViewWillAppear = NO; //this work if superclass is UITableviewcontroller
self.contentSizeForViewInPopover = CGSizeMake(200,100);
}
-(void)viewWillDisappear:(BOOL)animated
{
self.contentSizeForViewInPopover = CGSizeMake(250,200);
[super viewWillDisappear:YES];
}
ADD
Check some useful similar to your issue:-
Animate popoverContentsize when pushing navigation view controller in popover on iPad
UIPopoverController automatically resizing to max height on pushViewController
push a new tableViewController in a uipopovercontroller causes the popover to be resized
Popover with embedded navigation controller doesn't respect size on back nav
I am new in iOS development and I am trying to show a popover when tap a barButtomItem. So far I have this:
-(IBAction)shareButtonPressed :(id)sender{
UIViewController *popoverViewController = [[UIViewController alloc] initWithNibName:#"ShareOptionsViewController" bundle:nil];
popoverViewController.contentSizeForViewInPopover = CGSizeMake(319, 422);
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverViewController];
[self.popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}
This code works and when I tap the correct button, the popover is showed. The problem is when the popOver is showed, if I tap again the button, the app crashes. I know is because it is trying to alloc a variable that is already created. I think the solution should be disable the button when the popover loads but I don't know how to activate again once the popover is dismissed.
Am I right? How can I enable the button when the popOver is dismissed?
First you should disabled the button in your shareButtonPressed method.
-(IBAction)shareButtonPressed :(id)sender{
UIViewController *popoverViewController = [[UIViewController alloc] initWithNibName:#"ShareOptionsViewController" bundle:nil];
popoverViewController.contentSizeForViewInPopover = CGSizeMake(319, 422);
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverViewController];
[self.myButton setEnabled:NO];
[self.popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}
Then you make your view controller comply with the UIPopoverControllerDelegate protocol. After that you implement the popoverControllerDidDismissPopover method.
-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
if (!self.myButton.enabled) // Just to make sure the button is disabled.
[self.myButton setEnabled:YES];
}
Just check if instance is already created.
-(IBAction)shareButtonPressed:(id)sender
{
if (self.popoverController == nil) {
self.popoverController = alloc/init...;
[_popoverController present...];
} else {
if (self.popoverController.popoverVisible)
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController = nil;
}
}
There is a possibility, it will be garbage collected before animation completes, yielding invalid user experience. Then, if possible, I would pre-create the instance in viewDidLoad or contructor or so and then just present and dismiss as needed, checking visible property.
Look at this "event --> responsible" list:
1) Button pressed --> MainViewController - IBAction
2) Popover shown --> MainViewController - IBAction
3) Button disabled --> MainViewController - IBAction
4) Popover dissmis --> popoverDelegate - popoverControllerDidDismissPopover
5) Button enabled --> popoverDelegate - popoverControllerDidDismissPopover
So, if the delegate of your popover is the MainViewController, you would have access to the button, if it is a different class you'll probably have to set up a protocol in order to enable the button back.
Tell me if you need any further explanations...
Just modify your code like this...
-(IBAction)shareButtonPressed :(id)sender{
UIViewController *popoverViewController = [[UIViewController alloc] initWithNibName:#"ShareOptionsViewController" bundle:nil];
popoverViewController.contentSizeForViewInPopover = CGSizeMake(319, 422);
if(self.popoverController)
{
[self.popovercontroller dismissPopoverAnimated:YES];
self.popovercontroller = nil;
}
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverViewController];
[self.popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}
you can detect if the popover is shown and dismissed:
- (IBAction)shareButtonPressed :(id)sender{
if (self.popoverController) {
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController = nil;
} else {
UIViewController *popoverViewController = [[UIViewController alloc] initWithNibName:#"ShareOptionsViewController" bundle:nil];
popoverViewController.contentSizeForViewInPopover = CGSizeMake(319, 422);
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverViewController];
[self.popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}
}
or if you just want to disable the button:
- (IBAction)shareButtonPressed :(id)sender{
...
UIButton *button = (UIButton *)sender;
button.enabled = NO;
}
but in this case you will have to detect when the user close the popOver to enable the button again. You can do this adopting the UIPopoverController delegate (see documentation) in your class
How to add the UIView into UIPopoverViewController
-(void) buttonAction:(id)sender {
//build our custom popover view
UIViewController* popoverContent = [[UIViewController alloc]
init];
UIView* popoverView = [[UIView alloc]
initWithFrame:CGRectMake(0, 0, 200, 300)];
popoverView.backgroundColor = [UIColor whiteColor];
popoverContent.view = popoverView;
//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover =
CGSizeMake(200, 300);
//create a popover controller
self.popoverController = [[UIPopoverController alloc]
initWithContentViewController:popoverContent];
//present the popover view non-modal with a
//refrence to the button pressed within the current view
[self.popoverController presentPopoverFromRect:popoverButton.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
//release the popover content
[popoverView release];
[popoverContent release];
}
This will add the view to your UIPopoverViewController
You can check the following link below to get some idea as to how to do this
Are there examples of how to use UIPopoverController on iOS?
Try the below Code:
if (mycontroller == nil) {
MyController * viewController = [[MyController alloc] initWithNibName:#"ControllerView" bundle:nil];
mycontroller = viewController;
PopoverController = [[UIPopoverController alloc] initWithContentViewController:self.mycontroller];
PopoverController.delegate = self;
}
[PopoverController presentPopoverFromRect:CGRectMake(260, 120, 10, 10) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
I am using WEPopover in my app to pop up a popover controller containg some buttons,it works when i put wepopover controller action in tabbarIteam but i need to get popover when i click UIButton.How to do this this is my barbuttonitem code for displaying popover.
-(IBAction)_clickbtnAccount:(id)sender
{
if (!self.popoverController)
{
UIViewController *contentViewController = [[pageAccount alloc]initWithNibName:#"pageAccount" bundle:nil];
self.popoverController = [[[popoverClass alloc] initWithContentViewController:contentViewController] autorelease];
self.popoverController.delegate = self;
self.popoverController.passthroughViews = [NSArray arrayWithObject:self.navigationController.navigationBar];
[self.popoverController presentPopoverFromBarButtonItem:sender
permittedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown)
animated:YES];
[contentViewController release];
}
else
{
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController = nil;
}
}
-(IBAction)_clickbtnAccount:(id)sender
{
if (!self.popoverController)
{
UIButton *senderButton = (UIButton *)sender;
[self.popoverController presentPopoverFromRect:[senderButton frame]
inView:#"pageAccount"
permittedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown)
animated:YES];
}
else
{
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController = nil;
}
}
So you don't want to display a WEPopoverfrom an UIBarButtonItem but from an UIButton, right?!
Just use
- (void)presentPopoverFromRect:(CGRect)rect
inView:(UIView *)view
permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections
animated:(BOOL)animated;
instead of
- (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)item
permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections
animated:(BOOL)animated;
and pass in the rect of your UIButton.
Taking your code as an example:
-(IBAction)_clickbtnAccount:(id)sender
{
if (!self.popoverController) {
UIViewController *contentViewController = [[pageAccount alloc]initWithNibName:#"pageAccount" bundle:nil];
self.popoverController = [[[popoverClass alloc] initWithContentViewController:contentViewController] autorelease];
self.popoverController.delegate = self;
self.popoverController.passthroughViews = [NSArray arrayWithObject:self.navigationController.navigationBar];
UIButton *senderButton = (UIButton *)sender;
[self.popoverController presentPopoverFromRect:[senderButton frame]
inView:self.view
permittedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown)
animated:YES];
[contentViewController release];
} else {
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController = nil;
}
}
WEPopover is basically used to show multiple options inside a popover with much control using UITableview.
This is how i use WEPopOver to show popup on a button click
-(void)showPopOver:(id)sender{
if (self.popoverController) {
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController = nil;
}
PopOverTable *contentViewController = [[PopOverTable alloc] initWithStyle:UITableViewStylePlain];
contentViewController.delegatePopoverItemSelectedDelegate=self;
rectForPopover = [self.view convertRect:btn.bounds fromView:btn];
self.popoverController = [[[popoverClass alloc] initWithContentViewController:contentViewController] autorelease];
if ([self.popoverController respondsToSelector:#selector(setContainerViewProperties:)]) {
[self.popoverController setContainerViewProperties:[self improvedContainerViewProperties]];
}
self.popoverController.delegate = self;
[self.popoverController presentPopoverFromRect:rectForPopover
inView: self.view
permittedArrowDirections:(UIPopoverArrowDirectionAny)
animated:YES];
}
With the accepted solution I get rotation issues for the WEPopover under iOS 7: Basically it was pointing to a custom button in the UIBarButtonItem on the left of the navigation bar but on rotation the pop up moves to the right side of the screen and stays there on further rotations.
I am using a popover view to present a large amount of flags of which the your can select.
There is something wrong with my code since soon after I open this popover memory is not released (the viewcontroller "flagsViewController" is ok and clean, it does init and release each and every item inside of it.
What am I doing wrong? How can I free memory as soon as the popover is closed?
-(void)presentFlags
{
[self.popoverController dismissPopoverAnimated:YES];
FlagsViewController *controller = [[FlagsViewController alloc]
initWithNibName:#"FlagsViewController"
bundle:[NSBundle mainBundle]] ;
UINavigationController *container = [[UINavigationController alloc] initWithRootViewController:controller];
UISegmentedControl *ctrl = [[UISegmentedControl alloc] initWithItems:segmentedItems];
ctrl.frame = CGRectMake(0, 6, 500, 30);
[ctrl addTarget:self action:#selector(changeSeg:) forControlEvents:UIControlEventValueChanged];
ctrl.segmentedControlStyle = UISegmentedControlStyleBar;
//ctrl.momentary = YES;
ctrl.tintColor = [UIColor darkGrayColor];
UIImage *theImage = [UIImage imageNamed:#"highlight_country.png"];
[ctrl setImage:theImage forSegmentAtIndex:0];
[container.navigationBar addSubview:ctrl];
[ctrl release];
//
//create a popover controller
self.popoverController = [[[UIPopoverController alloc]
initWithContentViewController:container] autorelease];
[container release];
[popoverController setPopoverContentSize:CGSizeMake(500, 600)];
//present the popover view non-modal with a
//refrence to the button pressed within the current view
[popoverController presentPopoverFromRect:CGRectMake(popoverArrowPossition, 0.0, 0.0, 52.0) inView:super.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[controller release];
}
Working with the UIPopoverController has been pretty difficult but I solved this problem by doing the following setting the Delegate of the Popover Controller to self (popoverController.delegate = self) and adding the UIPopoverControllerDelegate Protocol to your Class Header
Next, I implemented the - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController delegate method and here I released the popoverController and set it to nil.
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
[self.popoverController release];
self.popoverController = nil;
}
Please note: This delegate method won't be called if you dismiss the popover via code (i.e. using dismissPopoverAnimated), it'll only be called if this User dismisses it by tapping outside the popover etc.