15

I have a button I have created programmatically within a view controller. Once the button is pressed I want it to to use a method to create the popover programmatically.

The button which is created in the ViewDidLoad in my view controller.m

 UIView *moreFundInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 540, 620)];
[self.view addSubview:moreFundInfoView];
[moreFundInfoView setBackgroundColor:[UIColor RMBColor:@"b"]];

btnContact = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];


[btnContact setFrame:CGRectMake(390, 575, contactButton.width, contactButton.height)];
 btnContact.hidden = NO;
[btnContact setTitle:@"Contact" forState:(UIControlStateNormal)];
[moreFundInfoView addSubview:btnContact];

[btnContact addTarget:self action:@selector(showContactDetails:) forControlEvents:UIControlEventTouchUpInside];

Then I have the method I use when the button is pressed.

-(void) showContactDetails: (id) sender
{
UIViewController *popoverContent = [[UIViewController alloc]init];

UIView *popoverView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 300)];

[popoverView setBackgroundColor:[UIColor RMBColor:@"b"]];

popoverContent.view = popoverView;

popoverContent.contentSizeForViewInPopover = CGSizeMake(200, 300);

UIPopoverController *contactPopover =[[UIPopoverController alloc] initWithContentViewController:popoverContent];

[contactPopover presentPopoverFromRect:btnContact.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES ];

[contactPopover setDelegate:self];

}

What am I missing here? Cause it runs fine, but as soon as I click the button the app crashes. I think it is a delegate issue, but I am not sure. Any advice would be appreciated.

Pawan Joshi
  • 1,581
  • 3
  • 20
  • 40
Berns
  • 159
  • 1
  • 1
  • 12
  • 1
    could you give us the crash log ? It would help knowing where the problem is – Valentin Rocher Feb 08 '13 at 11:00
  • @ValentinRocher still a bit of a n00b, but this is the output when it crashes. appname-[UIPopoverController dealloc] reached while popover is still visible./-[UIPopoverController dealloc] reached while popover is still visible. – Berns Feb 08 '13 at 11:10
  • Hi I managed to fix it, well, too an extent anyway. I just changed the property of the UIPopoverController to strong and it works, just it's pointing down instead of up, but it displays at least and the app doesn't crash. – Berns Feb 08 '13 at 11:26

4 Answers4

33

I think this code will help you. You are certainly missing delegate methods

ViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navigationController];
popover.delegate = self;
popover.popoverContentSize = CGSizeMake(644, 425); //your custom size.
[popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections: UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionUp animated:YES];

Just make sure you are not forgetting UIPopover Delegate methods or else application will definitely crash. It is mandatory.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Pratik Somaiya
  • 818
  • 7
  • 18
  • 3
    Use of UINavigationController is totally optional. You can skip creation of its object. Just for information. Have a happy coding. – Pratik Somaiya Jul 27 '13 at 09:47
2
 UIViewController *controller = [[UIViewController alloc] init];
 [view removeFromSuperview]; //view is a view which is displayed in a popover
 controller.view = view;
 UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:controller];
 popover.delegate = self;
[popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
PVCS
  • 3,831
  • 1
  • 16
  • 10
1

All I had to do was change the property from "retain" to "strong" in the .h file and it works, stopped the app from crashing.

Berns
  • 159
  • 1
  • 1
  • 12
1

Yes, changing property for "retain" to "strong" makes you to hold your picker view object. I think the problem with your code was, UIPopoverController object gets deallocated automatically when method finishes. making strong property gets you to strongly point an object.

Pawan Joshi
  • 1,581
  • 3
  • 20
  • 40