Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a UIPopoverController

I'm creating a popover in Xcode 4.3. I don't get any error messages but when I click build and run and I press the button that is supposed to open the popover screen, the app crashes and highlights in green a line in my code and says:

Thread 1: breakpoint 2.1..

What does this mean and how can I fix it?

- (IBAction)popOver
{
    SecondView *secondview = [[SecondView alloc] init];
    UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:secondview];    

    [popover setDelegate:self];    

    [popover presentPopoverFromRect:CGRectMake(801, 401, 300, 200) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
    [popover setPopoverContentSize:CGSizeMake(300, 200)];   
}

Thanks in advance.

like image 717
user1253207 Avatar asked Nov 28 '22 03:11

user1253207


2 Answers

Few suggestions here...

First, are you using Automatic Reference Counting (ARC)? if yes you need to have an instance variable to control your UIPopoverController. For example:

@property (strong, nonatomic) UIPopoverController* popover;

if you are not using ARC create a retain one:

@property (retain, nonatomic) UIPopoverController* popover;

In the first case you have to do this because if not, ARC will release for you the just created popover at the end of your IBAction. In addition you do this to have a reference for your popover. See NOTE. This is valid also if you don't use ARC.

NOTE At some point you could have the necessity to release the popover. For example in - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController do

self.popover = nil;

Then, if your SecondView is a UIViewController call it SecondViewController. But this is a simple naming suggestion.

Finally, the correct way for display the popover from the action sender (e.g a UIButton), instead of hardcode positions, could be:

- (IBAction)openPopover:(id)sender
{
    // create the popover here...

    UIButton* senderButton = (UIButton*)sender;

    [popover setPopoverContentSize:CGSizeMake(300, 200)];
    [popover presentPopoverFromRect:senderButton.bounds inView:senderButton permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}
like image 79
Lorenzo B Avatar answered Dec 10 '22 08:12

Lorenzo B


-(IBAction)showpop:(id)sender
{
UIPopoverController *pop = [[UIPopoverController alloc]initWithContentViewController:popoverview];
[pop setDelegate:self];
[pop presentPopoverFromRect:popbutton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

}
like image 38
Nadella Ravindra Avatar answered Dec 10 '22 07:12

Nadella Ravindra