首先,我想说的是,我对 ipad/ipod/iphone 开发以及objective-c 真的很陌生。
话虽如此,我正在尝试使用 Xcode 和 IB 开发一个针对 iPad 的小型应用程序,基本上,我有一个表格,对于表格中的每个 UITableViewCell,我向附件视图添加了一个包含图像的按钮。
这是代码:
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;
到目前为止,一切都很好,现在的问题是当用户点击单元格的附件视图上的按钮时,我希望出现一个 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];
这段代码的问题在于它在应用程序视图的顶部显示了 Popover,而在调试时我看到了“rect”的值,它们是:
x = 267
y = 13
所以我认为很明显为什么 PopOver 在视图上显示得如此之高,所以我的问题是,我怎样才能让 PopOver 的正确值出现在单元格的附件视图上的按钮下方?
此外,如您所见,我告诉它为“inView:”属性使用“cell.accessoryView”,可以吗?