【问题标题】:Unable to set up button with tag无法设置带有标签的按钮
【发布时间】:2016-06-23 15:22:42
【问题描述】:

我有一个我想使用的单元格,我只想要一个单元格中的按钮的插座,这样我就可以显示一个下拉菜单。我正在尝试使用此代码制作带有标签的按钮

var dropdownButton: UIButton = (PostCell.viewWithTag(200) as! UIButton)

我在 cellForRowAtIndexPath 中使用它,但问题是我不断收到错误提示

“在“UIView”类型上使用实例成员“viewWithTag”;您的意思是使用“UIView”类型的值吗?

【问题讨论】:

  • viewWIthTag 是一种可用于 UIVIew 类型对象的方法。 POSTCell 不是 UIView。

标签: ios xcode swift button tags


【解决方案1】:

尝试使用代码作为

var dropdownButton: UIButton = (postCell.contentView.viewWithTag(200) as? UIButton)

您正在使用PostCell 似乎您是通过类名直接访问它。尝试使用UITableViewCell 的对象进行访问。 UITableViewCell 上的所有视图都放在它的 contentView 上,所以从那里访问按钮。

还可以使用? 将对象作为按钮获取,因为如果您使用!,如果由于某种原因找不到标签200 的对象,应用程序可能会崩溃。

为防止应用崩溃,请使用?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多