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

时间秒杀一切 提交于 2019-12-10 12:31:40

问题


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.


回答1:


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.




回答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];



回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/14770980/how-to-display-a-popover-programmatically-from-a-uibutton-that-is-also-created-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!