从此 UIButton 呈现的弹出窗口中的 UIButtons 获取 UIButton 的标签

Get a UIButton's tag from the UIButtons which are in the popover presented by this UIButton

标题看起来很长,但我想做的很简单。

我有几个相同的按钮排成一排,并将它们的标签设置为 0...n。单击它们中的任何一个(例如第二个)将弹出一个弹出视图,其中有几个按钮代表不同的选项(A、B、C、D)。如果我们点击选项 B,我想做的是将第二个按钮的标题变成 B。

问题是弹出视图不知道哪个按钮显示它,因为所有的弹出视图都是同一个 UIViewController class 的实例。所以我正在考虑通过将它们的标签设置为不同的值来区分 n 个按钮。但是,我不知道如何从此 UIButton 显示的弹出框内的按钮获取 UIButton 的标签。

非常感谢!

这就是我在 swift 中解决这个问题的方法。我将在 popoverViewController 中声明一个委托并有一个方法,例如

protocol popOverViewControllerDelegate: class {
 func popOverViewController(controller: PopOverViewController,
    didSelectItem buttonTitle: String)
}

然后我会在popOver

的UIButton中添加一个目标动作
button.addTarget(self, action: "selected:",forControlEvents:.TouchUpInside)

select 方法会将发送者传递给它

func selected(sender:UIButton){
  delegate?.popOverViewController(self, didSelectItem: sender.currentTitle)
//dismiss viewcontroller
}

记得申报

weak var delegate: popOverViewControllerDelegate!

现在有调用 popOver 的 viewcontroller,该委托的子类并实现它的方法。每当 popOver 中的按钮被 selected 时,将调用此方法并传递该方法的 currentTitle。您现在可以使用它来设置调用 popOver 的按钮的 currentTitle。 如果您在最后一部分需要进一步的帮助,请询问。

我通过添加 属性 tagNumber 解决了这个问题,因此在实例化 popoverViewController 的 class 之后,我将实例的 tagNumber 设置为发送者的标签。然后我将 tagNumbersender.currentTitle 一起发回。这样,弹出窗口的呈现器就可以知道 UIButton 的标题和标签号。