2017-02-27 38 views
1

我有子类UITableViewCell。在这里,我做了一个按钮的出口。当该按钮被点击时,它应该在不同的ViewController中执行一个函数。我曾经尝试这样做:如何在UITableViewCell中为不同的ViewController设置UIButton的IBAction?

override func awakeFromNib() { 
    super.awakeFromNib() 
    reloadTableView.addTarget(self, action: #selector(multiplayerChannelView.tappedOnReloadTableView), for: .touchUpInside) 
} 

然而,这种崩溃,出现此错误:

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

的功能存在,没有错字。为什么这不起作用?这个问题看起来是一样的,只有它不是写在迅速3.0 How to set Action for UIButton in UITableViewCell

multiplayerChannelView是持有UITableView的viewcontroller。我用UITableViewCell subclassed得到了一个分离的.swift文件。

+0

是'multiplayerChannelView'是你的控制器您身在何处的tableView填充数据? –

+0

Yesssssssssssss – NoKey

回答

4

中的cellForRowAtIndexPath

cell.your_button.addTarget(self, action: #selector(self.tappedButton(sender:)), for: .touchUpInside); 
在同一UIVeiwController

,随时随地添加此定义函数如下

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

我得到的类型UITableViewCell的错误值没有成员X.但是,我已经在我的UITableViewCell中设置了一个插座。 – NoKey

+0

cell.your_button.addTarget(self,action:#selector(self.tappedButton(sender :)),for:.touchUpInside); – Moin

+0

谢谢,最短的答案是有效的。 – NoKey

1

写下面的代码文件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 
} 
} 
+0

不,这不是它是如何工作的我得到所有类型的错误 – NoKey

+0

请尝试在你的答案给予更多细节 - 代码片段将有所帮助 –

1

您可以通过在你的tableViewCell类创建delegate做到这一点。

protocol CustomTableViewCellDelegate { 
func buttonPressed() 
} 

然后初始化委托象下面这样的tableViewCell

var delegate: CustomTableViewCellDelegate? 

并把下面的代码在你的tableViewCell

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

在按钮点击请检查是否委托按钮动作不为零,请在cellForRowAtIndex方法中设定cell.delegate = self方法

在最后只需添加代码按钮动作在你的类,你必须使用customTableViewCell

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

} 

希望它能为你工作!

+0

我不知道在哪里把所有的功能。当我尝试添加cell.delegate = self时,我得到一个错误:类型UITableViewCell的值没有成员'委托'。在哪里把var委托,在什么VC?以及在哪里放置IBAction功能?我试图在两个viewcontrollers中添加var委托,仍然没有运气 – NoKey

+0

@JasperVisser我已经更新了我的答案 –

相关问题