How to display a popover programmatically from a uibutton that is also created programmatically (Not using interface builder)
Asked Answered
Y

4

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.

Yankeeism answered 8/2, 2013 at 10:55 Comment(3)
could you give us the crash log ? It would help knowing where the problem isHeldentenor
@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.Yankeeism
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.Yankeeism
R
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.

Represent answered 28/2, 2013 at 6:47 Comment(1)
Use of UINavigationController is totally optional. You can skip creation of its object. Just for information. Have a happy coding.Represent
W
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];
Wincer answered 30/12, 2014 at 16:55 Comment(0)
Y
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.

Yankeeism answered 12/2, 2013 at 5:26 Comment(1)
if doing this fixed your issue, you can accept your own answer.Cantabile
P
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.

Pleione answered 13/1, 2014 at 11:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.