gpt4 book ai didi

objective-c - 如何从 UITableViewCell.accessoryView 调用 PopOver Controller ?

转载 作者:搜寻专家 更新时间:2023-10-30 19:54:34 28 4
gpt4 key购买 nike

首先我想说我对 ipad/ipod/iphone 开发真的很陌生,对 objective-c 也是如此。

话虽如此,我正在尝试使用 Xcode 和 IB 开发一个针对 iPad 的小型应用程序,基本上,我有一个表,对于表中的每个 UITableViewCell,我向 accessoryView 添加了一个按钮,其中包含一个图片。

代码如下:

UIImage *img = [UIImage imageNamed:@"myimage.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, img.size.width, img.size.height);
button.frame = frame; // match the button's size with the image size

[button setBackgroundImage:img forState:UIControlStateNormal];

// set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;

到目前为止一切顺利,现在的问题是我希望在用户点击单元格的 accessoryView 上的按钮时出现 PopOver 控件。

我在 tableView 的“accessoryButtonTappedForRowWithIndexPath”上试过这个:

UITableViewCell *cell = [myTable cellForRowAtIndexPath:indexPath];
UIButton *button = (UIButton *)cell.accessoryView;

//customViewController is the controller of the view that I want to be displayed by the PopOver controller
customViewController = [[CustomViewController alloc]init];

popOverController = [[UIPopoverController alloc]
initWithContentViewController: customViewController];

popOverController.popoverContentSize = CGSizeMake(147, 122);

CGRect rect = button.frame;

[popOverController presentPopoverFromRect:rect inView:cell.accessoryView
permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

这段代码的问题在于它在应用程序 View 的顶部显示了 Popover,在调试时我看到了“rect”的值,它们是:

x = 267
y = 13

所以我认为很明显为什么 PopOver 在 View 上显示得如此之高,所以我的问题是,我怎样才能让 PopOver 的正确值出现在单元格的 accessoryView 的按钮下方?

此外,如您所见,我告诉它为“inView:”属性使用“cell.accessoryView”,这样可以吗?

最佳答案

尝试使用 button.bounds 而不是 button.frame 因为 rect 是相对于 inView 的。

另请注意,如果单元格位于屏幕底部附近,则弹出框可能无法以正确的大小显示,因为您迫使箭头朝上方向移动。您应该手动处理或只使用 Any。

关于objective-c - 如何从 UITableViewCell.accessoryView 调用 PopOver Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2823342/

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