【问题标题】:Popover from UITableviewcell not workingUITableviewcell 的弹出窗口不起作用
【发布时间】:2016-10-03 16:50:13
【问题描述】:

我有动态的tableview,我有 6 个单元格。

问题陈述:

现在,我想显示一个弹出框,它应该正好出现在所选单元格的右侧,并且箭头向左指向单元格。

我做了什么:

1) 创建了PopUpViewController.m/.h
2) 创建了一个从单元格到此 popUpViewController 的“Present as popover”segue。
3) 目前故事板中的锚点设置为tableview,因此弹出框从左上角显示。

看完之后,

1) 我为单元创建了一个XTableViewCell.m/.h 类。
2) 在单元格的右端创建一个小按钮,并将其放在XTableViewCell.h 文件中,以将此按钮用作锚点。

我的问题是,我已经在情节提要中完成了所有这些。

将我的tableViewController 类视为YTableViewController.h/.m

1) 我应该在哪里以及如何使用锚按钮将我的弹出框锚定到我的要求?

2) 默认情况下,故事板中的锚点设置为单元格名称。它会被我的按钮覆盖吗?

3) 我应该在哪里配置 UIPopoverPresentationController 参数?

4) 如何在 iPhone 中将弹出窗口显示为“弹出窗口”而不是全屏视图控制器?

请帮忙。

【问题讨论】:

  • 你能告诉我一些你写的代码来呈现popover的代码吗?
  • 除了如上添加 .m/.h 文件外,其余所有更改都在情节提要本身中。

标签: ios objective-c uitableview uipopovercontroller


【解决方案1】:

首先,您应该从您的视图控制器中创建一个弹出框segue,其中包含您的表格视图。您可以通过控制从文档大纲中的黄色视图控制器图标拖动到要弹出的视图控制器来执行此操作。通过单击 segue 并打开属性检查器并将其添加到 segue 标识符字段,为其提供一个 segue 标识符。像“Popover”这样的东西现在可以工作了。

接下来,您应该从 segue 标识符字段下方的锚视图圈拖动到您的视图控制器的视图(我们稍后会在代码中对此进行调整)。

您应该向您的UITableViewCell 子类添加一个按钮。然后在您的cellForRowAtIndexPath: 中,您可以为按钮添加一个目标,例如:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *cellIdentifier = [menuItems objectAtIndex:indexPath.row]; // I'm not sure what's going on here but hopefully you do
    RegisterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    [cell.PopoverAnchorButton addTarget:self action:@selector(presentPopover:) forControlEvents:UIControlEventTouchUpInside];

    // Configure the cell...
    return cell;

}

presentPopover: 方法添加到您的视图控制器:

- (void) presentPopover:(UIButton *)sender
{
    [self performSegueWithIdentifier:@"Popover" sender:sender];
}

现在,您可以在视图控制器的 prepareForSegue: 中更改源视图

您可以获得对UIPopoverPresentationController 的引用并检查源视图是否是您的UITableViewCell 子类的实例。如果是这样,请将源视图调整为其按钮。

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"Popover"] && segue.destinationViewController.popoverPresentationController) {
        UIPopoverPresentationController *popController = segue.destinationViewController.popoverPresentationController;
        popController.sourceView = sender;
        popController.delegate = self;
        // if you need to pass data you can access the index path for the cell the button was pressed by saying the following
        CGPoint location = [self.tableView convertPoint:sender.bounds.origin fromView:sender];
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
        // do something with the indexPath
    }
}

如果您希望视图控制器即使在实现UIPopoverPresentationControllerDelegate 方法时也始终显示为弹出框:

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
    return UIModalPresentationStyleNone;
}

此时,您可能会收到警告。这可以通过告诉编译器您符合UIPopoverPresentationControllerDelegate 协议来消除。例如:

@interface MyTableViewController () <UITableViewDataSource, UITableViewDelegate,UIPopoverPresentationControllerDelegate>

【讨论】:

  • 由于某些原因,在尝试您的答案 Main.storyboard: Couldn't compile connection: property=anchorView destination=> 我将锚点设置为按钮故事板,我得到上述错误。我已经对我的单元格进行了子类化,并将类名赋予故事板中的单元格。
  • 嗨 beyowulf,谢谢您的详细解释。我编写了或多或少相同的代码,但出现了上述错误。按照你所说的,让锚点指向 Tableview 本身,然后在 prepareForSegue 中进行更改。通过您的实现,我的弹出窗口从单元格的最左边开始,即使我的按钮位于右端。有什么输入可以做同样的事情吗?
  • 为您的按钮添加目标的发布代码和该操作的代码。此外,您可以设置断点或记录prepareForSegue 中的发件人类型,如果不是UIButton,则设置不正确。
  • 嗨,我已经把我的代码放在这里了,还有一个问题。 stackoverflow.com/questions/37663578/… 我想,我会切换到静态表格单元格:(
  • 嗨,我刚刚查过了。您正在为未调用按钮操作编写 .code。米检查更多。这就是与专业人士合作的好处! .赞成你的回答。
猜你喜欢
  • 1970-01-01
  • 2012-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-17
  • 2019-01-14
  • 2014-12-21
  • 1970-01-01
相关资源
最近更新 更多