如何在 swift 4 中使用集合视图内的按钮更新标签文本

How to update a label text with button inside a collection view in swift 4

我在 table 视图中有一个集合视图。集合视图单元格中有两个加减按钮。现在我必须更新 table 视图之外的加号减号按钮操作上的标签。提前致谢。

我必须通过单击加号减号按钮来更新插槽:(标签)。

我用委托协议尝试过类似的东西。

我在我的集​​合视图中声明了一个委托 class。

 protocol SlotsCollectionViewCellDelegate: NSObjectProtocol {
func didTapOnIncrement(Int: Int)
func didTapOnDecrement(Int: Int)

}

//之后,

 var delegate: SlotsCollectionViewCellDelegate?


 @IBAction func plusBtnAction(_ sender: Any) {
     self.delegate?.didTapOnIncrement(Int: cartCount)
  }

   @IBAction func minusBtnAction(_ sender: Any) {
     delegate?.didTapOnDecrement(cell: self)
   }

在我的主视图控制器中

extension MainViewController: SlotsCollectionViewCellDelegate {

func didTapOnIncrement(Int: Int) {
       cartSlot_lbl.text = Int.description
     }

     func didTapOnDecrement(Int: Int) {
         cartSlot_lbl.text = Int.description
     }

}

我认为代表是正确的选择。如果它不起作用,请解释原因并显示一些代码,您可能忘记设置委托引用。

无论如何,这里还有一些想法:

  • 您可以使用反应模式,这样您就可以创建一个中继来存储您的当前值,通过提供输入(时间等)来操纵它们并从 Class 订阅它们"Spot:" 标签实现的地方。每当您的模型更改时,您的 Spot Label 也会更改。

  • 您还可以使用通知来实现某些功能。基本上来说,与使用响应式模式的区别并不大,您只需要自己关心 "notify" 部分即可。假设你在存储整个状态(选择 dates/times、插槽等)的地方应用了类似单例模​​式的东西,你可以这样做:

extension Notification.Name {
    static let modelDidChange = Notification.Name("modelDidChange")
}
// where your model lies
struct YourModel {
    var slots: Int = 0

    static var singletonInstance: YourModel = YourModel() {
        // use the didSet block to react to changes made to the model
        didSet {
            // send a notification so all subscriber know something has changed
            NotificationCenter.default.post(.modelDidChange)
        }
    }
}

class YourViewControllerWhereTheLabelIs: UIViewController {
    // ...
    var slotLabel: UILabel?

    // ...
    init() {
        // wherever you initialize your viewcontroller,
        // you could also do it in viewWillAppear
        // subscribe to the notification to react to changes
        NotificationCenter.default.addObserver(self, selector: #selector(modelDidChange), name: .modelDidChange, object: nil)
    }

    deinit {
        // just don't forget to unsubscribe
        NotificationCenter.default.removeObserver(self)
    }

    @objc func modelDidChange() {
        // update your label here, this is called whenever YourModel.singletonInstance is changed
        self.slotLabel?.text = YourModel.singletonInstance.slots
    }
}


希望对您有所帮助或给您一个想法。如果我能提供更多帮助,请告诉我。

如果我没理解错的话,每次你按 + 或 - 时你都想更新插槽标签。在我看来,实现这一目标的最简单和最快的方法是使用 NotificationCenter.default.post

在按钮操作的集合视图单元格中写入:

NotificationCenter.default.post(name: Notification.Name("postAction"), object: numberToIncreaseOrDecrease)

在您有插槽标签的 MainViewController 中,将此代码添加到已加载的视图中:

NotificationCenter.default.addObserver(self, selector: #selector(updateSlotValue(_:)), name: NSNotification.Name("postAction"), object: nil)

并从视图中加载添加此功能:

 @objc func updateSlotValue(_ notification: Notification) {
    let value = notification.object as! Int
    cartSlot_lbl.text.text = value
}