ios 如何从同样以编程方式创建的 uibutton 以编程方式显示弹出框(不使用界面构建器)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14770980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:13:47  来源:igfitidea点击:

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

iosuibuttonuipopover

提问by Berns

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

在我的视图 controller.m 的 ViewDidLoad 中创建的按钮

 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.

我在这里缺少什么?因为它运行良好,但是一旦我单击按钮,应用程序就会崩溃。我认为这是一个委托问题,但我不确定。任何意见,将不胜感激。

回答by Pratik Somaiya

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.

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

回答by PVCS

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

回答by Berns

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.

我所要做的就是将 .h 文件中的属性从“保留”更改为“强”,并且它可以工作,阻止了应用程序崩溃。

回答by Pawan Joshi

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.

是的,将“retain”的属性更改为“strong”可以让您保持选择器视图对象。我认为您的代码的问题是, UIPopoverController 对象在方法完成时自动解除分配。制作强大的属性可以让你强烈地指向一个对象。