如何从也以编程方式创建的uibutton中以编程方式显示弹出窗口(不使用界面生成器) [英] How to display a popover programmatically from a uibutton that is also created programmatically (Not using interface builder)

查看:84
本文介绍了如何从也以编程方式创建的uibutton中以编程方式显示弹出窗口(不使用界面生成器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已在视图控制器中以编程方式创建的按钮.按下按钮后,我希望它使用一种方法以编程方式创建弹出窗口.

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.

在我的视图控制器的ViewDidLoad中创建的按钮.m

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.

推荐答案

我认为这段代码将为您提供帮助.您肯定缺少委托方法

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

请确保您没有忘记UIPopover Delegate方法,否则应用程序肯定会崩溃.这是强制性的.

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

这篇关于如何从也以编程方式创建的uibutton中以编程方式显示弹出窗口(不使用界面生成器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆