2017-02-27 5 views
1

私はUITableViewCellをサブクラス化しました。ここで私はボタンのアウトレットを作った。そのボタンをクリックすると、別のViewController内の関数を実行するはずです。私はこれを試みた:別のViewControllerのUITableViewCellで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を保持するビューコントローラです。 UITableViewCellをサブクラス化して別の.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がありません。 – NoKey

+0

cell.your_button.addTarget(self、action:#selector(self.tappedButton(sender :)):.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() 
} 

天気を委譲がnilでない確認]をクリックし、あなたのtableViewCell

var delegate: CustomTableViewCellDelegate? 

ボタンを

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

あなたtableViewCellクラス内のコードの下に入れて、ボタンのアクションのために、以下のようなあなたのデリゲートを初期化します、cell.delegate = selfcellForRowAtIndexに設定してください。

最後に

あなたがcustomTableViewCellクラスを使用していたところ、ちょうどあなたのCustomTableViewCellDelegateは、以下のように見える

extension ViewController : CustomTableViewCellDelegate { 
     func buttonPressed() { 
      // Perfom your code on button action 
     } 
} 

あなたのクラスで、ボタンアクションのコードを追加します。

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の値に 'delegate'というメンバーがありません。どこのVCで、代理人を置く場所は? IBAction関数をどこに置くべきですか?両方のviewcontrollersでvar delegateを追加しようとしましたが、まだ運がありません。 – NoKey

+0

@JasperVisser私は自分の答えを更新しました –

関連する問題