swift中对与delegate的使用

swift中对与delegate的使用

为了保证delegate使用后不会导致内存溢出,可以做如下两种方式:

1、protocol声明时不指出base class:

protocol ZXSegmentControlDelegate {
    func didSelect(index:NSInteger)
}

使用时就需要手动将delegate置为nil

self.segmentCtl?.delegate = nil

2、protocol声明时指出base class:

protocol ZXSegmentControlDelegate: UIViewController {
    func didSelect(index:NSInteger)
}

class ZXSegmentControl: UIView {
    weak var delegate:ZXSegmentControlDelegate?

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s