Help us understand the problem. What is going on with this article?

【Swift / Obj-C】SwiftからObjective-CへDelegate通知を送る

More than 3 years have passed since last update.

背景

 Swiftの普及に伴って、新規iOSアプリ開発の際だけでなく、既存のObjective-Cのプロジェクトにも積極的にSwiftの導入が推進されるようになってきており、自分もObjective-Cで書かれたプロジェクトに対して、共存する形でSwiftを導入することになった。
 そんな中、既存のViewControllerクラス(Obj-C)に対して、新しいカスタムCell(Swift)を導入し、カスタムCell側で定義したUIButtonのタップを検知してViewController側で処理したいケースが出てきたので、備忘録としてSwiftからObjective-CへのDelegate通知を送った流れをメモ。

環境

Xcode 7.2
Swift 2.1.1

実装方法

1. CustomCell側

 ① UITableViewDelegateを継承
 ② CustomCellDelegate?(Optional型)を定義
 ③ Objective-C側で利用するプロトコルに関数を定義
 ④ CustomCellDelegate?に③の関数を定義

CustomCell.swift
import UIKit

//③
@objc protocol CustomCellDelegate{
    func pushButton(sender: UIButton) 
}

//①
class CustomCell: UITableViewCell,UITableViewDelegate {

    //②
    var delegate: CustomCellDelegate?

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

        let button = UIButton(frame: CGRectMake(0,0,200,40))
        button.backgroundColor = UIColor.orangeColor()
        button.setTitle("ボタン", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.addTarget(self, action: "pushButton:", forControlEvents: .TouchUpInside)
        self.addSubview(button)
    }

    func pushButton(button: UIButton){
        //④
        self.delegate?.pushButton(button)
    }

}

2. ViewController側

① デリゲートを宣言する
② カスタムセルを読み込み、delegateを設定する
③ デリゲートメソッドを記述する

ViewController.m
~
//①
@interface ViewController()<CustomCellDelegate>
@end

~

//②
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if(cell == nil){
            cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
            cell.delegate = self;

        }

~

//③
- (void)pushButton:(UIButton *)sender{
    //処理
}

補足

あらかじめObjective-CからSwiftへのアクセスはできるようになっているものとします

asa_s
Unity Engineer (2016 - now), Scala Engineer (2016 - now), iOS Engineer (2014 - 2016)
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした