Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 398
Berns Avatar asked Feb 08 '13 10:02

Berns


4 Answers

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.

like image 190
Pratik Somaiya Avatar answered Nov 09 '22 22:11

Pratik Somaiya


 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];
like image 2
PVCS Avatar answered Nov 09 '22 22:11

PVCS


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.

like image 1
Berns Avatar answered Nov 09 '22 22:11

Berns


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.

like image 1
Pawan Joshi Avatar answered Nov 09 '22 23:11

Pawan Joshi