Swift 如何为其他ViewController的UITableViewCell中的UIButton设置iAction?

Swift 如何为其他ViewController的UITableViewCell中的UIButton设置iAction?,swift,Swift,我已将UITableViewCell子类化。这里我为一个按钮做了一个插座。单击该按钮后,它应该在不同的ViewController中执行一个函数。我试过这个: override func awakeFromNib() { super.awakeFromNib() reloadTableView.addTarget(self, action: #selector(multiplayerChannelView.tappedOnReloadTableView), for: .touch

我已将UITableViewCell子类化。这里我为一个按钮做了一个插座。单击该按钮后,它应该在不同的ViewController中执行一个函数。我试过这个:

override func awakeFromNib() {
    super.awakeFromNib()
    reloadTableView.addTarget(self, action: #selector(multiplayerChannelView.tappedOnReloadTableView), for: .touchUpInside)
}
但是,此错误会导致崩溃:

[test.CreateChannelCell tappedOnReloadTableView]:发送到实例0x7fc1198b5200的无法识别的选择器

该函数存在,没有输入错误。为什么这不起作用?这个问题看起来是一样的,只是它不是用swift 3.0编写的


multiplayerChannelView是保存UITableView的viewcontroller。我得到了一个单独的.swift文件,其中包含UITableViewCell子类。

在文件VC2中编写下面的代码

 import UIKit
 class tblCell : UITableViewCell
{

@IBOutlet weak var btnAction: UIButton!
@IBAction func btnAction(_ sender: AnyObject)
{
    print(sender.tag) // you can identify your cell from sender.tag value

    // notification is fire here and call notification from VC1
    NotificationCenter.default.post(name:NSNotification.Name(rawValue: "buttonAction"), object: nil)
}
}

class VC2: UIViewController,UITableViewDelegate,UITableViewDataSource
{

@IBOutlet weak var tblview: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()
    tblview.delegate = self
    tblview.dataSource = self
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


// MARK: -  Table veiw
func tableView(_ tblBlog: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 10

}

func tableView(_ tblBlog: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{

    let cell : tblCell = tblview.dequeueReusableCell(withIdentifier: "Cell") as! tblCell

    cell.btnAction.tag = indexPath.row

    return cell
    //
}

}

在文件VC1中写入以下代码

class VC1: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool)
{
    // this notification is call when it fire from VC2
    NotificationCenter.default.addObserver(self, selector: #selector(ButtonClick), name: NSNotification.Name(rawValue: "buttonAction"), object: nil)
}
func ButtonClick()
{
    // code when button is clicked you wanto perfrom
}
}

在VC2文件中写入以下代码

 import UIKit
 class tblCell : UITableViewCell
{

@IBOutlet weak var btnAction: UIButton!
@IBAction func btnAction(_ sender: AnyObject)
{
    print(sender.tag) // you can identify your cell from sender.tag value

    // notification is fire here and call notification from VC1
    NotificationCenter.default.post(name:NSNotification.Name(rawValue: "buttonAction"), object: nil)
}
}

class VC2: UIViewController,UITableViewDelegate,UITableViewDataSource
{

@IBOutlet weak var tblview: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()
    tblview.delegate = self
    tblview.dataSource = self
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


// MARK: -  Table veiw
func tableView(_ tblBlog: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 10

}

func tableView(_ tblBlog: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{

    let cell : tblCell = tblview.dequeueReusableCell(withIdentifier: "Cell") as! tblCell

    cell.btnAction.tag = indexPath.row

    return cell
    //
}

}

在文件VC1中写入以下代码

class VC1: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool)
{
    // this notification is call when it fire from VC2
    NotificationCenter.default.addObserver(self, selector: #selector(ButtonClick), name: NSNotification.Name(rawValue: "buttonAction"), object: nil)
}
func ButtonClick()
{
    // code when button is clicked you wanto perfrom
}
}

您可以通过在
tableViewCell
类中创建
delegate
来完成此操作

protocol CustomTableViewCellDelegate {
func buttonPressed ()
}
然后在
tableViewCell

var delegate: CustomTableViewCellDelegate?
对于按钮操作,请在
tableViewCell
类中的代码下方输入

@IBAction func cellButtonPressed (sender : UIButton) {
            if (self.delegate != nil) {
                self.delegate?.buttonPressed()
    }
extension ViewController : CustomTableViewCellDelegate {
        func buttonPressed () {
            // Perfom your code on button action
        }
}
单击按钮检查委托是否为非零,请在
cellforrowatinex
方法中设置
cell.delegate=self

在最后一个示例中,只需在使用了
customTableViewCell
类的类中添加按钮操作代码

@IBAction func cellButtonPressed (sender : UIButton) {
            if (self.delegate != nil) {
                self.delegate?.buttonPressed()
    }
extension ViewController : CustomTableViewCellDelegate {
        func buttonPressed () {
            // Perfom your code on button action
        }
}
您的
CustomTableViewCellDelegate
如下所示:

import UIKit

protocol CustomTableViewCellDelegate {
    func buttonPressed ()
}

class CustomTableViewCell: UITableViewCell {

    var delegate: CustomTableViewCellDelegate?


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

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

        // Configure the view for the selected state
    }

    @IBAction func cellButtonPressed (sender : UIButton) {
        if (self.delegate != nil) {
            self.delegate?.buttonPressed()
    }

}

希望它对你有用

您可以通过在
tableViewCell
类中创建
delegate
来完成

protocol CustomTableViewCellDelegate {
func buttonPressed ()
}
然后在
tableViewCell

var delegate: CustomTableViewCellDelegate?
对于按钮操作,请在
tableViewCell
类中的代码下方输入

@IBAction func cellButtonPressed (sender : UIButton) {
            if (self.delegate != nil) {
                self.delegate?.buttonPressed()
    }
extension ViewController : CustomTableViewCellDelegate {
        func buttonPressed () {
            // Perfom your code on button action
        }
}
单击按钮检查委托是否为非零,请在
cellforrowatinex
方法中设置
cell.delegate=self

在最后一个示例中,只需在使用了
customTableViewCell
类的类中添加按钮操作代码

@IBAction func cellButtonPressed (sender : UIButton) {
            if (self.delegate != nil) {
                self.delegate?.buttonPressed()
    }
extension ViewController : CustomTableViewCellDelegate {
        func buttonPressed () {
            // Perfom your code on button action
        }
}
您的
CustomTableViewCellDelegate
如下所示:

import UIKit

protocol CustomTableViewCellDelegate {
    func buttonPressed ()
}

class CustomTableViewCell: UITableViewCell {

    var delegate: CustomTableViewCellDelegate?


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

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

        // Configure the view for the selected state
    }

    @IBAction func cellButtonPressed (sender : UIButton) {
        if (self.delegate != nil) {
            self.delegate?.buttonPressed()
    }

}

希望它对你有用

将其添加到cellForRowAtIndexPath中

cell.your_button.addTarget(self, action: #selector(self.tappedButton(sender:)), for: .touchUpInside);
在同一UIVeiwController中的任意位置定义函数,如下所示

func tappedButton(sender : UIButton){
// Your code here
}

将其添加到cellForRowAtIndexPath中

cell.your_button.addTarget(self, action: #selector(self.tappedButton(sender:)), for: .touchUpInside);
在同一UIVeiwController中的任意位置定义函数,如下所示

func tappedButton(sender : UIButton){
// Your code here
}

multiplayerChannelView
是您在tableView中填充数据的控制器吗?是的
multiplayerChannelView
是您在tableView中填充数据的控制器吗?是的,我不知道将所有函数放在哪里。当我尝试添加cell.delegate=self时,我得到一个错误:UITableViewCell类型的值没有成员“delegate”。var委托放在哪里,在什么位置?IBAction函数放在哪里?我试着在两个viewcontrollers中添加var委托,但是没有一个luckI不知道把所有函数放在哪里。当我尝试添加cell.delegate=self时,我得到一个错误:UITableViewCell类型的值没有成员“delegate”。var委托放在哪里,在什么位置?IBAction函数放在哪里?我尝试在两个ViewController中添加var委托,仍然没有luckNo这不是它的工作方式我得到所有类型的错误请尝试在您的答案中提供更多细节-代码片段将有帮助否这不是它的工作方式我得到所有类型的错误请尝试在您的答案中提供更多细节-代码片段将有助于我获得UITableViewCell类型的错误值没有成员X。但是,我已在我的UITableViewCell.cell.your_button.addTarget(self,action:#选择器(self.tappedButton(sender:)),for:.touchUpInside)中设置了一个插座;我得到UITableViewCell类型的错误值,但它没有成员X。但是,我已在我的UITableViewCell.cell.your_button.addTarget(self,action:#selector(self.tappedButton(sender:))中设置了一个出口,用于:.touchUpInside);