【问题标题】:Provide action for button in the xib file using swift3使用 swift3 为 xib 文件中的按钮提供操作
【发布时间】:2017-09-19 11:16:05
【问题描述】:

提供:我的 xib 中有一个按钮 还附上了与此相关的 swift 文件。

问题:此单元格有一个按钮,需要在单击按钮时显示 ViewController。此单元格附加到另一个 ViewController 中的表视图。我想在“BOOK”按钮上实现一个动作,以便单击新的视图控制器应该打开。我无法做到这一点,任何人都可以建议我应该做些什么吗?

代码:

import UIKit

class HotelBookingCell: UITableViewCell {

    @IBOutlet weak var BookbtnOutlet: UIButton!
    override func awakeFromNib() {
        super.awakeFromNib()
        BookbtnOutlet.layer.cornerRadius = 7
        // Initialization code
    }

    @IBAction func bookbtn1(_ sender: Any) {

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

【问题讨论】:

  • 在你的 UIviewcontroller 中调用这个bookbtn1
  • 在您的 VC 中,在 cellforrowatindex 方法中在按钮上添加目标
  • 你能出示一下代码cellforrowatindex
  • public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 5; } public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cellid", for: indexPath) as! BookHotellCell 返回单元格 } @Anbu.Karthik

标签: ios swift3 xib


【解决方案1】:

删除tableviewcell类中的以下代码

/*
@IBAction func bookbtn1(_ sender: Any) {

} */

并添加到您的 UIviewcontroller cellForRowAtIndexPath

cell. BookbtnOutlet.tag = indexpath.row
cell. BookbtnOutlet.addTarget(self, action: #selector(self. bookbtn1(_:)), for: .touchUpInside);

在同一个 UIVeiwController 中的任何地方定义如下函数

func bookbtn1(_ sender : UIButton){
// call your segue code here
}

【讨论】:

  • 这是一个意大利面条代码 :) 虽然,这个解决方案肯定会工作
  • @SabaZehra - 你击中的地方,你需要与此相关的帮助
  • 我可以得到你的邮件ID吗? @Anbu.Karthik
  • 但是如果我有两个按钮,这个代码就不起作用了。
【解决方案2】:

为此创建协议的可能解决方案之一:

protocol HotelBookingCellDelegate: class {
     // you can add parameters if you want to pass. something to controller
     func bookingCellBookButtonTouched()  
}

那你就是细胞类

class HotelBookingCell: UITableViewCell {
     // add a propery
     public weak var delegate: HotelBookingCellDelegate?

     @IBAction func bookbtn1(_ sender: Any) {
          delegate?.bookingCellBookButtonTouched()
     }
}

在你的 cellForRowAtIndexPath 中

cell.delegate = self

之后,在您签署此协议的控制器中,实施它

extension YourViewController: HotelBookingCellDelegate {
    func bookingCellBookButtonTouched() {
      // Do whatever you want
    }
}

【讨论】:

    【解决方案3】:

    您可以在单元格类中创建一个委托协议,然后将委托设置为等于 viewcontroller,其中 tablview 单元格将显示。然后单击委托函数将被调用,您将获得视图控制器的操作,您可以在其中推送或弹出视图控制器或您想要的任何其他操作。

    示例代码 - 单元类

    protocol ButtonDelegate {
        func buttonClicked()
    }
    
    weak var delegate : ButtonDelegate?
    @IBAction func bookbtn1(_ sender: Any) {
        delegate?.buttonClicked()
    }
    

    现在在 View Controller 中符合协议-“ButtonDelegate”,设置

    cell.delegate = self
    

    然后实现方法“buttonClicked()”

    单击按钮时,您将在 buttonClicked() 中获取操作。

    【讨论】:

    • Thankx 会尽力让您知道的。
    【解决方案4】:

    这里有两种可能的解决方案

    1) 您可以在 cellForRowAtIndexPath 中为该按钮添加目标,如下代码所示

    cell.bookbtn1.tag = indexPath.row cell.bookbtn1.addTarget(self, action: #selector(self.bookbtn1(sender:)), for: .touchUpInside)

    2) 另一个解决方案是在您的主视图控制器中,您可以像这样在 viewDidLoad 中添加通知中心观察者

    NotificationCenter.default.addObserver(self, selector: #selector(self.bookbtn1ClickedFromCell), name: NSNotification.Name(rawValue: BUTTON_CLICK), object: nil)
    

    并通过该方法实现方法并在另一个视图控制器中导航

    func bookbtn1ClickedFromCell()
    {
        //navigate to another vc
    }
    

    您在 UITableViewCell 文件中实现的操作方法会像这样发布此通知

    @IBAction func bookbtn1(_ sender: Any) {
    
      NotificationCenter.default.post(name: Notification.Name(rawValue: BUTTON_CLICK), object: self)
    }
    

    所以它会在你的主视图控制器中调用 bookbtn1ClickedFromCell,你可以从这里导航到另一个视图控制器

    您应该在 viewWillDisappeardeinit 方法中删除观察者

     deinit {
        NotificationCenter.default.removeObserver(self)
    }
    

      override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        NotificationCenter.default.removeObserver(self)
    

    }

    【讨论】:

    • 添加删除观察者也在视图中消失
    • 是的,需要在 viewWillDisappear 中删除观察者,或者您也可以在 deinit 中删除观察者
    猜你喜欢
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多