Ios 使用swift3为xib文件中的按钮提供操作
提供:我的xib中有一个按钮 与此相关的swift文件也随附 问题:此单元格有一个按钮,需要在单击按钮时显示ViewController。此单元格附着到另一个ViewController中的表视图。我想在按钮“BOOK”上实现一个操作,以便单击新的视图控制器时打开。我做不到这一点,有人能建议我做点什么吗 代码:Ios 使用swift3为xib文件中的按钮提供操作,ios,swift3,xib,Ios,Swift3,Xib,提供:我的xib中有一个按钮 与此相关的swift文件也随附 问题:此单元格有一个按钮,需要在单击按钮时显示ViewController。此单元格附着到另一个ViewController中的表视图。我想在按钮“BOOK”上实现一个操作,以便单击新的视图控制器时打开。我做不到这一点,有人能建议我做点什么吗 代码: 删除tableviewcell类中的以下代码 /* @IBAction func bookbtn1(_ sender: Any) { } */ class HotelBookingC
删除tableviewcell类中的以下代码
/*
@IBAction func bookbtn1(_ sender: Any) {
} */
class HotelBookingCell: UITableViewCell {
// add a propery
public weak var delegate: HotelBookingCellDelegate?
@IBAction func bookbtn1(_ sender: Any) {
delegate?.bookingCellBookButtonTouched()
}
}
并将其添加到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
}
您可以在cell类中创建一个委托协议,然后将委托设置为viewcontroller,tablview单元格将在其中显示。然后单击将调用委托函数,您将获得视图控制器的操作,您可以在其中推送或弹出视图控制器或您想要的任何其他操作 示例代码-单元类
protocol ButtonDelegate {
func buttonClicked()
}
weak var delegate : ButtonDelegate?
@IBAction func bookbtn1(_ sender: Any) {
delegate?.buttonClicked()
}
现在在视图中,控制器符合协议“ButtonDelegate”,设置
然后实现方法“buttonClicked()”
单击按钮时,您将在buttonClicked()中获得操作。可能的解决方案之一是为此创建协议:
protocol HotelBookingCellDelegate: class {
// you can add parameters if you want to pass. something to controller
func bookingCellBookButtonTouched()
}
那你是手机课吗
/*
@IBAction func bookbtn1(_ sender: Any) {
} */
class HotelBookingCell: UITableViewCell {
// add a propery
public weak var delegate: HotelBookingCellDelegate?
@IBAction func bookbtn1(_ sender: Any) {
delegate?.bookingCellBookButtonTouched()
}
}
在你的牢房里
cell.delegate = self
之后,在您为该协议签名的控制器中,实现它
extension YourViewController: HotelBookingCellDelegate {
func bookingCellBookButtonTouched() {
// Do whatever you want
}
}
这里有两种可能的解决方案 1) 您可以在单元格中为该按钮添加目标,如下面的代码所示 cell.bookbtn1.tag=indexPath.row cell.bookbtn1.addTarget(self,action:#选择器(self.bookbtn1(sender:)),for:.touchUpInside) 2) 另一个解决方案是在主视图控制器中,您可以像这样在viewDidLoad中添加Notification center observer
NotificationCenter.default.addObserver(self, selector: #selector(self.bookbtn1ClickedFromCell), name: NSNotification.Name(rawValue: BUTTON_CLICK), object: nil)
@IBAction func bookbtn1(_ sender: Any) {
NotificationCenter.default.post(name: Notification.Name(rawValue: BUTTON_CLICK), object: self)
}
和实现方法,并从此方法导航到另一个视图控制器
func bookbtn1ClickedFromCell()
{
//navigate to another vc
}
在UITableViewCell文件中实现的实际方法如下所示发布此通知
NotificationCenter.default.addObserver(self, selector: #selector(self.bookbtn1ClickedFromCell), name: NSNotification.Name(rawValue: BUTTON_CLICK), object: nil)
@IBAction func bookbtn1(_ sender: Any) {
NotificationCenter.default.post(name: Notification.Name(rawValue: BUTTON_CLICK), object: self)
}
因此,它将在主视图控制器中被称为bookbtn1ClickedFromCell,您可以从此导航到另一个视图控制器
您应该在视图将消失或取消显示方法中删除观察者
deinit {
NotificationCenter.default.removeObserver(self)
}
或
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}在UIviewcontroller、VC、CellForRowatinex方法中调用此
bookbtn1
one在按钮上添加目标可以显示代码CellForRowatinex
public func tableView(tableView:UITableView,numberOfRowsInSection:Int)->Int{return 5;}public func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{let cell=tableView.dequeueReusableCell(标识符为:cellid,for:indexPath)as!BookHotellCell return cell}@Anbu.KarthikThankx将尝试让您知道。这是一个意大利面代码:)尽管,这个解决方案肯定会work@SabaZehra-在你罢工的地方,你需要与此相关的帮助吗?我可以获取你的邮件id吗@Anbu.KarthikBut如果我有两个按钮,此代码将不起作用。@Parcker-您可以显示您尝试过的代码添加删除观察者也在视图中消失也可以是正确的需要删除视图中的观察者将消失,或者您也可以删除Denit中的观察者