0

I'm trying to create a popover with the following code:

//.h
@interface LoginViewController : UIViewController <UIPopoverControllerDelegate>
....
@end

//.m
- (IBAction)popoverTest:(id)sender
{
    UIViewController *popoverContent = [[UIViewController alloc] init];
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];

    UILabel *nameLabel = [[UILabel alloc] init];  //edited, fixed UILabel allocation
    nameLabel.text = @"Person's name";
    [nameLabel sizeToFit];
    nameLabel.frame = CGRectMake(10, 10, nameLabel.frame.size.width, nameLabel.frame.size.height);
    [myView addSubview:nameLabel];

    popoverContent.view = myView;

    popoverContent.contentSizeForViewInPopover = CGSizeMake(300, 300);

    UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
    popoverController.delegate = self;

    [popoverController presentPopoverFromRect:((UIButton *)sender).frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

    NSLog(@"ran all code");
}

I created a UIView, put a label as a subview and then assigned my view to the UIViewController.view. Then I created a popover controller, sized the popover controller, set the delegate and presented it from the button's frame.

I receive a SIGABRT and the app crashes.

Is there something I'm missing?

EDIT: I fixed the UILabel allocation. The problem is always there.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Padin215
  • 7,444
  • 13
  • 65
  • 103
  • put a breakpoint within your IBAction and see where it crashes. enable also [exceptions](http://stackoverflow.com/questions/4961770/run-stop-on-objective-c-exception-in-xcode-4) – Lorenzo B Feb 22 '12 at 17:29

3 Answers3

3

Your code is quite strange.

For example why do you create a view and do you add it to the content view of your popover?

Then, you have to make attention to memory leaks. There a lot of them in your code.

That said, here a simple example for display a UIViewcontroller within a UIPopoverController.

- (IBAction)yourAction:(id)sender

    UIButton* senderButton = (UIButton*)sender;

    UIViewController* yourController = [[UIViewController alloc] init];

    UIPopoverController* pop = [[UIPopoverController alloc] initWithContentViewController:yourController];
    pop.delegate = self;
    pop.popoverContentSize = CGSizeMake(300, 300);    

    self.popover = pop;

    [pop presentPopoverFromRect:senderButton.bounds inView:senderButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    pop.passthroughViews = nil;

    [yourController release];
    [pop release];
}

where self.popover is a @property with a retain policy. In this manner in UIPopoverControllerDelegate methods (or wherever you want), you can release your popover and/or dismiss it.

Hope it helps.

P.S. Check the code because I've written by hand.

Edit

Usually when you create a popover, its content view controller is or a custom UIViewController or a UINavigationController (in this case you want to take advantage of its navigation bar).

For example, instead of the simple UIViewController, you could create a custom one

//.h
@interface CustomUIViewController : UIViewController

@end

//.m
@implementation CustomUIViewController 

// other code here...

- (void)viewDidLoad
{
   // here you are sure that the view has been loaded in memory (alternatively override loadView method), so
   UIView* greenView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
   green.backgroundColor = [UIColor greenColor];
   [self.view addSubview:greenView];
   [greenView release];
}

@end

and use it within a a popover

- (IBAction)yourAction:(id)sender

    UIButton* senderButton = (UIButton*)sender;

    CustomUIViewController* yourController = [[CustomUIViewController alloc] init];

    // same as before...
}
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • thanks for the suggestion, i'll consider it. and i guess i should have stated that i am using ARC in this project. – Padin215 Feb 22 '12 at 20:18
  • re-read your answer, and you did suggest making my UIPopoverController into an instance variable which is what fixed the issue – Padin215 Feb 22 '12 at 20:21
1

solved my issue from this question.

short answer: ARC doesn't retain the UIPopoverController.

i made it an instance variable and works just fine.

Community
  • 1
  • 1
Padin215
  • 7,444
  • 13
  • 65
  • 103
0

The problem seems to be the following:

UILabel *nameLabel;
nameLabel.text = @"Person's name";

nameLabel is uninitialized. It points to random memory, and when calling -setText: through the text-property, your application crashes.

Apart from this, you have a few memory leaks. Remember: If you call alloc, you have to release it somewhere. (I'm not sure how this works with the new automatic alloc/release).

jsadfeew
  • 2,247
  • 1
  • 18
  • 25