【问题标题】:Disable a button from different ViewController - Swift 3禁用来自不同 ViewController 的按钮 - Swift 3
【发布时间】:2017-12-15 09:51:09
【问题描述】:

所以我想启用/禁用该 tableViewCell 中的按钮:

class CommonPromoCell: UITableViewCell {
@IBOutlet weak var optionBtnCommon: UIButton

当另一个按钮从其他 tableViewCell 启用/禁用时

 class PromoButtonCell: UITableViewCell {
 @IBOutlet weak var option1Btn: UIButton!
 @IBOutlet weak var option2Btn: UIButton!
 @IBOutlet weak var option3Btn: UIButton!

因此,我主要希望选择选项 1 到选项 3 中的一个来启用optionBtnCommon,而当上述 3 个按钮中的任何一个都没有被选择来禁用它时。

我尝试过类似的东西

var viewController: PromoButtonCell?

// TODO : DISABLE COMMON BUTTON WHEN NO OTHER IS SELECTED
        if (viewController?.option1Btn.isSelected)! || (viewController?.option2Btn.isSelected)! || (viewController?.option3Btn.isSelected)! {
            optionBtnCommon.isEnabled = true
        } else {
            optionBtnCommon.isEnabled = false
        }

如果写在#selector#selectorfuncself.optionBtnCommon.addTarget

已编辑:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let currentPackage = self.packages[indexPath.section]
    switch indexPath.row {

    case 1:
        let cell = tableView.dequeueReusableCell(withIdentifier: "PromoButtonCell") as! PromoButtonCell
        cell.populate(currentPackage)
        cell.contentView.tag = (indexPath.section + 1) * 10
        return cell

    case 2:
        let cell = tableView.dequeueReusableCell(withIdentifier: "CommonPromoCell") as! CommonPromoCell
        cell.populate(currentPackage, forPayment: self.payments[self.paymentIndex].type)
        cell.optionBtnCommon.tag = indexPath.section
        return cell

谢谢。

【问题讨论】:

  • var viewController: PromoButtonCell? 的实例保存在哪里以及如何保存?
  • 我已经在CommonPromoCell: UITableViewCell 中声明了它。忘了说当强制解开它时它会崩溃
  • @FagadaruMarcel 请给我看你的TableView。你不能像上面评论中所说的那样做这样的事情。
  • 我的 mainTableVIew 到底是什么?
  • @dahiya_boy 我已经编辑了我的问题,如果这就是你要找的东西

标签: ios swift uitableview swift3


【解决方案1】:

如果用户可以更改 option1Btn 和 option2Btn 的状态,您将需要在 PromoButtonCell 和视图控制器之间实现某种事件处理,以修改 CommonPromoCell 的状态。

我将创建一个协议,以便PromoButtonCell 可以通知您的 viewController 单元格状态已更改。假设您使用 promoButtonCell:UITableViewCell didSelectButton: UIButton 之类的事件创建了 PromoButtonCellDelegate

那么viewController就可以实现那个协议了,当接收到事件时,尝试访问另一个CommonPromoCell,来改变它的状态。

听起来怎么样?

【讨论】:

  • 您好,我已经在PromoButtonCell viewcontroller protocol PromoButtonCellDelegate: class { var promoButtonCell: UITableViewCell? {get} var didSelectButton: UIButton? {get} }weak var commonDelegate: PromoButtonCellDelegate?CommonPromoCell 中创建了扩展名extension CommonPromoCell: PromoButtonCellDelegate { var promoButtonCell: UITableViewCell? { <#code#> } var didSelectButton: UIButton? { <#code#> } },这样好吗?谢谢
猜你喜欢
  • 2018-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
相关资源
最近更新 更多