【问题标题】:Dynamically / Programmatically assign IBAction to a button以动态/编程方式将 IBAction 分配给按钮
【发布时间】:2018-10-22 11:23:24
【问题描述】:

对于我的项目,我创建了一个排序菜单,它以编程方式创建水平滚动的按钮。但是,我正在寻找将 IBAction(特别是写入 Firebase 数据库的操作)动态分配给这些按钮的方法…… 标记按钮和使用条件会起作用吗?

我的代码是

Food.swift - 每个菜单项的属性集

class Food
{
    var title = ""
    var featuredImage: UIImage
    var color: UIColor

    init(title: String, featuredImage: UIImage, color: UIColor)
    {
        self.title = title
        self.featuredImage = featuredImage
        self.color = color
    }

    static func fetchFoods() -> [Food]
    {
        return [
            Food(title: "                   Biscuits", featuredImage: UIImage(named: "f1")!, color: UIColor(red: 63/255.0, green: 71/255.0, blue: 80/255.0, alpha: 0.6)),
            Food(title: "                   Sandwich", featuredImage: UIImage(named: "f2")!, color: UIColor(red: 63/255.0, green: 71/255.0, blue: 80/255.0, alpha: 0.6)),
            Food(title: "             Veg Sandwich", featuredImage: UIImage(named: "f3")!, color: UIColor(red: 63/255.0, green: 71/255.0, blue: 80/255.0, alpha: 0.6)),

        ]
    }
}

UICollectionCell 快速

import UIKit
class FoodCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var featuredImageView: UIImageView!
    @IBOutlet weak var backgroundColorView: UIView!
    @IBOutlet weak var foodButton: UIButton!
    var food: Food? {
        didSet {
            self.updateUI()
        }
    }
    private func updateUI()
    {
        if let food = food {
            featuredImageView.image = food.featuredImage
            backgroundColorView.backgroundColor = food.color
            foodButton.setTitle("\(food.title)", for: .normal)
        } else {
            featuredImageView.image = nil
            backgroundColorView.backgroundColor = nil
            foodButton.setTitle("", for: .normal)
        }
    }
}

我可以添加到 Food.swift,

 var buttonTag = "" 

然后将标签分配给单个按钮。 在 UICollectionCell.swift 中,附加标签。使用 If...else... 条件将标签与单个 IBAction {ref?.child...")

匹配

谢谢大家!!!

ps.还有另一个用于水平排列菜单项的 swift 文件..

【问题讨论】:

  • 您应该考虑在按钮上使用addTarget(_ target: Any?, action: Selector, for controlEvents: UIControl.Event) 方法。
  • 顾名思义,Interface Builder Action 仅适用于IB。只需添加一个目标。

标签: ios swift ibaction


【解决方案1】:

标签是一种方法,但最简单的方法可能是在每个标签上调用addTarget(_:action:for:) 以设置其操作,例如

button.addTarget(button,
                 action: #selector(buttonFunctionThatDoesSomething:),
                 for: .touchUpInside) // or .primaryActionTriggered

第一个参数是要调用其选择器(第二个参数)的对象。 #selector() 语法真的很挑剔;您可能需要阅读 this SO answer 以找出正确的调用方式。

【讨论】:

  • 谢谢!听起来我不是在重复这个问题:如何将 addTarget 函数分配给以编程方式创建的特定按钮?我想您需要使每个按钮对象都独一无二?按钮的标题 - 以编程方式创建 - 能完成这项工作吗?
【解决方案2】:

来自苹果文档https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ImplementingACustomControl.html。 :

 button.addTarget(self, action: #selector(funcName), for: .touchUpInside)

然后:

 @objc func funcName()  {
       //do what u want in here
    }

【讨论】:

  • 谢谢!听起来我不是在重复这个问题:如何将 addTarget 函数分配给以编程方式创建的特定按钮?我想您需要使每个按钮对象都独一无二?按钮的标题 - 以编程方式创建 - 能完成这项工作吗?
  • 我不确定我是否理解正确,但您可以将按钮标题作为参数传递给您的函数并做您想做的事情
猜你喜欢
  • 2012-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-02
  • 2010-10-26
  • 1970-01-01
相关资源
最近更新 更多