代码之家  ›  专栏  ›  技术社区  ›  Saba Zehra

使用swift3为xib文件中的按钮提供操作

  •  2
  • Saba Zehra  · 技术社区  · 7 年前

    提供:我的xib中有一个按钮 enter image description here

    代码:

    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
        }
    
    }
    
    4 回复  |  直到 7 年前
        1
  •  5
  •   Anbu.Karthik    7 年前

    删除tableviewcell类中的以下代码

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

    并将CellforRowatingIndexPath添加到UIviewcontroller中

    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
    }
    
        2
  •  2
  •   T. Pasichnyk    7 年前

    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
  •  1
  •   Prateek Varshney    7 年前

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

    示例代码-单元类

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

    现在,在视图中控制器符合协议-“ButtonDelegate”,设置

    cell.delegate = self
    

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

        4
  •  1
  •   shalini barot    7 年前

    1) 您可以在中添加此按钮的目标 cellForRowAtIndexPath单元

    单间牢房bookbtn1.tag=indexPath。一行 单间牢房bookbtn1.addTarget(self,action:#选择器(self.bookbtn1(sender:)),用于:。触摸内部)

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

    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)
    }
    

    或者在 方法

     deinit {
        NotificationCenter.default.removeObserver(self)
    }
    

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