Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

george carpente

macrumors newbie
Original poster
Apr 2, 2011
8
0
Hi, somebody can help me implementing popover?

Here's my code:
PHP:
-(IBAction)popOver{
SecondViewController *secondView = [[SecondViewController alloc] init];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:secondView]; 
[popover setDelegate:self];
[popover presentPopoverFromRect:CGRectMake(0, 0, 330, 225) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:NO];
[popover setPopoverContentSize:CGSizeMake(330, 225)];
[secondView release];
}

I have button under UIScrollView. So when I push the button, popover should come up. BUT THIS DONT WORK!!! :( Popover comes up but in the top of the screen not behind the button.

What's wrong?
 
Hi, somebody can help me implementing popover?

Here's my code:
PHP:
-(IBAction)popOver{
SecondViewController *secondView = [[SecondViewController alloc] init];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:secondView]; 
[popover setDelegate:self];
[popover presentPopoverFromRect:CGRectMake(0, 0, 330, 225) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:NO];
[popover setPopoverContentSize:CGSizeMake(330, 225)];
[secondView release];
}

I have button under UIScrollView. So when I push the button, popover should come up. BUT THIS DONT WORK!!! :( Popover comes up but in the top of the screen not behind the button.

What's wrong?


presentPopoverFromRect:CGRectMake(0, 0, 330, 225) is the location in the view you are presenting the popover not the popover size. If you have a button that is the focus of the popover use presentPopoverFromBarButtonItem:.


Hope this helps
 
Last edited:
I've tried like this:

PHP:
-(IBAction)popOver{
SecondViewController *secondView = [[SecondViewController alloc] init];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:secondView]; 
[popover setDelegate:self];
[popover presentPopoverFromBarButtonItem:self permittedArrowDirections:UIPopoverArrowDirectionLeft animated:NO];
[popover setPopoverContentSize:CGSizeMake(330, 225)];
[secondView release];
}

But popover appears in the center of the screen.
 
I've tried like this:

PHP:
-(IBAction)popOver{
SecondViewController *secondView = [[SecondViewController alloc] init];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:secondView]; 
[popover setDelegate:self];
[popover presentPopoverFromBarButtonItem:self permittedArrowDirections:UIPopoverArrowDirectionLeft animated:NO];
[popover setPopoverContentSize:CGSizeMake(330, 225)];
[secondView release];
}

But popover appears in the center of the screen.

not self, your button.
 
I've tried this:
PHP:
//*.h file
UIButton *button;
@property(reatain)IBOutlet UIButton *button;

//*.m file
@synthesize button;
[popover presentPopoverFromBarButtonItem:button permittedArrowDirections:UIPopoverArrowDirectionLeft animated:NO];

And connected in IB "button" outlet and my button, but still no result :(
 
Still no result:
PHP:
//h file
-(IBAction)buttonClick:(id)sender;

//m file
-(IBAction)buttonClick:(id)sender{
[popover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionLeft animated:NO];  
}

What am I doing wrong?
 
//*.h file
UIButton *button;
@property(reatain)IBOutlet UIButton *button;

//*.m file
@synthesize button;
[popover presentPopoverFromBarButtonItem:button permittedArrowDirections:UIPopoverArrowDirectionLeft animated:NO];


Well, @property (nonatomics, retain) is the first mistake.
And u are trying to do a presentPopoverFromBarButtonItem..
U are trying to show it from a normal button, u didn't think of that while reading it?


What I did.
Was create a new XIB, and make a reference of that in my H file of where I want to present it.

Make an import first

Code:
#import "PopoverView.h"

// Add UIPopoverViewDelegate here.

UIPopoverController *popoverController;
IBOutlet PopoverView *popoverView;

Then in your .M File

Code:
 #pragma mark -
#pragma mark PopoverDelegate

- (IBAction)showPopoverTechnieker:(id)sender {
	if (popoverController==nil) {
		popoverController=[[UIPopoverController alloc] initWithContentViewController:popoverView];
		[popoverController presentPopoverFromRect:[sender frame] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
		popoverController.delegate=self;
	}
}

-(void)popoverControllerDidDismissPopover:(UIPopoverController *)controller {
	[popoverController release];
	popoverController=nil;
}

Something like that.
 
I've tried to do both presentPopoverFromBarButtonItem and presentPopoverFromRect.

Both examples gave equal results. I think the problem is that my button which calls popover under UIScrollView may be, I don't know, but really stuck.

By the way Sykte recommended me to use presentPopoverFromBarButtonItem.
 
I've tried to do both presentPopoverFromBarButtonItem and presentPopoverFromRect.

Both examples gave equal results. I think the problem is that my button which calls popover under UIScrollView may be, I don't know, but really stuck.

By the way Sykte recommended me to use presentPopoverFromBarButtonItem.


Hi George,


Sorry I couldn't get back to you.

In your header file declare an instance variable for a UIBarButton, also create a property for it but don't forget the IBOutlet. Also in your header create a method that will be fired when the button is tapped (shows your popover). Do not forget to include the IBAction with the method. Save your header file, now open IB or if you're using xcode 4 click on the xib. Drag a UIBarButton out, now hook up the Outlet & Action, save the xib. Go into your implementation and synthesize your UIBarButton then implement your method to show the popover
 
Hi George,


Sorry I couldn't get back to you.

In your header file declare an instance variable for a UIBarButton, also create a property for it but don't forget the IBOutlet. Also in your header create a method that will be fired when the button is tapped (shows your popover). Do not forget to include the IBAction with the method. Save your header file, now open IB or if you're using xcode 4 click on the xib. Drag a UIBarButton out, now hook up the Outlet & Action, save the xib. Go into your implementation and synthesize your UIBarButton then implement your method to show the popover

I work with a simple button, not UIBarButton, I've tried to make the way you recomended with a simple button, but program crashes.
 
The point is that there is no errors it just goes back to the desktop. I understand that self is like "this" in C++. Don't understand what sender is.

PHP:
//h file
UIButton *button;
@property(nonatomic,retain) IBOutlet UIButton *button;

//m file
-(IBAction)popOver:(id)sender{ 
SecondViewController *secondView = [[SecondViewController alloc] init]; 
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:secondView];  
[popover setDelegate:self]; 
/* I'cant use presentPopoverFromRect cause my button is under scroll and position is variable
[popover presentPopoverFromRect:CGRectMake(800,500,311,50) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:NO]; 
*/
[popover presentPopoverFromBarButtonItem:button
permittedArrowDirections:UIPopoverArrowDirectionLeft animated:NO]; 
[popover setPopoverContentSize:CGSizeMake(330, 225)]; 
[secondView release]; 
}
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.