wrong position of popover...

Discussion in 'iOS Programming' started by george carpente, Apr 2, 2011.

  1. macrumors newbie

    Joined:
    Apr 2, 2011
    #1
    Hi, somebody can help me implementing popover?

    Here's my code:
    PHP:
    -(IBAction)popOver{
    SecondViewController *secondView = [[SecondViewController allocinit];
    UIPopoverController *popover = [[UIPopoverController allocinitWithContentViewController:secondView]; 
    [
    popover setDelegate:self];
    [
    popover presentPopoverFromRect:CGRectMake(00330225inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:NO];
    [
    popover setPopoverContentSize:CGSizeMake(330225)];
    [
    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?
     
  2. Sykte, Apr 2, 2011
    Last edited: Apr 2, 2011

    macrumors regular

    Joined:
    Aug 26, 2010
    #2

    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
     
  3. thread starter macrumors newbie

    Joined:
    Apr 2, 2011
    #3
    I've tried like this:

    PHP:
    -(IBAction)popOver{
    SecondViewController *secondView = [[SecondViewController allocinit];
    UIPopoverController *popover = [[UIPopoverController allocinitWithContentViewController:secondView]; 
    [
    popover setDelegate:self];
    [
    popover presentPopoverFromBarButtonItem:self permittedArrowDirections:UIPopoverArrowDirectionLeft animated:NO];
    [
    popover setPopoverContentSize:CGSizeMake(330225)];
    [
    secondView release];

    But popover appears in the center of the screen.
     
  4. macrumors regular

    Joined:
    Aug 26, 2010
    #4
    not self, your button.
     
  5. thread starter macrumors newbie

    Joined:
    Apr 2, 2011
    #5
    How can I sender to the button? I'm rookie :) I've created this button in Interface Builder.
     
  6. macrumors regular

    Joined:
    Aug 26, 2010
    #6
    You will need to create a property and link it via an IBOutlet.
     
  7. thread starter macrumors newbie

    Joined:
    Apr 2, 2011
    #7
    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 :(
     
  8. thread starter macrumors newbie

    Joined:
    Apr 2, 2011
    #8
    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?
     
  9. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #9
    //*.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.
     
  10. thread starter macrumors newbie

    Joined:
    Apr 2, 2011
    #10
    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.
     
  11. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #11
    I just gave u the full code of how to do it from a button.. :/
     
  12. macrumors regular

    Joined:
    Aug 26, 2010
    #12

    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
     
  13. thread starter macrumors newbie

    Joined:
    Apr 2, 2011
    #13
    I work with a simple button, not UIBarButton, I've tried to make the way you recomended with a simple button, but program crashes.
     
  14. macrumors regular

    Joined:
    Aug 26, 2010
    #14
    You will need to post your errors, do you understand what sender was and self?
     
  15. george carpente, Apr 3, 2011
    Last edited: Apr 3, 2011

    thread starter macrumors newbie

    Joined:
    Apr 2, 2011
    #15
    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,retainIBOutlet UIButton *button;

    //m file
    -(IBAction)popOver:(id)sender
    SecondViewController *secondView = [[SecondViewController allocinit]; 
    UIPopoverController *popover = [[UIPopoverController allocinitWithContentViewController: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(330225)]; 
    [
    secondView release]; 

     

Share This Page