PDA

View Full Version : wrong position of popover...




george carpente
Apr 2, 2011, 09:54 AM
Hi, somebody can help me implementing popover?

Here's my code:

-(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?



Sykte
Apr 2, 2011, 10:08 AM
Hi, somebody can help me implementing popover?

Here's my code:

-(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

george carpente
Apr 2, 2011, 10:20 AM
I've tried like this:


-(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.

Sykte
Apr 2, 2011, 10:21 AM
I've tried like this:


-(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.

george carpente
Apr 2, 2011, 10:26 AM
How can I sender to the button? I'm rookie :) I've created this button in Interface Builder.

Sykte
Apr 2, 2011, 10:38 AM
How can I sender to the button? I'm rookie :) I've created this button in Interface Builder.

You will need to create a property and link it via an IBOutlet.

george carpente
Apr 2, 2011, 11:06 AM
I've tried this:

//*.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 :(

george carpente
Apr 2, 2011, 02:04 PM
Still no result:

//h file
-(IBAction)buttonClick:(id)sender;

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


What am I doing wrong?

jnoxx
Apr 2, 2011, 02:12 PM
//*.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


#import "PopoverView.h"

// Add UIPopoverViewDelegate here.

UIPopoverController *popoverController;
IBOutlet PopoverView *popoverView;

Then in your .M File

#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.

george carpente
Apr 2, 2011, 04:04 PM
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.

jnoxx
Apr 2, 2011, 04:51 PM
I just gave u the full code of how to do it from a button.. :/

Sykte
Apr 2, 2011, 07:24 PM
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

george carpente
Apr 3, 2011, 04:40 AM
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.

Sykte
Apr 3, 2011, 06:29 AM
I work with a simple button, not UIBarButton, I've tried to make the way you recomended with a simple button, but program crashes.

You will need to post your errors, do you understand what sender was and self?

george carpente
Apr 3, 2011, 07:35 AM
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.


//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];
}