gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 10:06:35 26 4
gpt4 key购买 nike

我有一个在 View Controller 中以编程方式创建的按钮。按下按钮后,我希望它使用一种方法以编程方式创建弹出窗口。

在我的 View Controller 中的 ViewDidLoad 中创建的按钮.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];

然后我有了按下按钮时使用的方法。

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

我在这里错过了什么?因为它运行良好,但只要我点击按钮,应用程序就会崩溃。我认为这是一个委托(delegate)问题,但我不确定。任何建议将不胜感激。

最佳答案

我认为这段代码会对你有所帮助。您肯定缺少委托(delegate)方法

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 方法,否则应用程序肯定会崩溃。这是强制性的。

关于ios - 如何从同样以编程方式创建的 uibutton 以编程方式显示弹出窗口(不使用界面构建器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14770980/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com