技术文章分类(180)

技术随笔(11)

swift delegate自定义

1,这里定义代理协议,并且调用delegate方法

import UIKit

protocol FaceDelegate:NSObjectProtocol{
    func selectedFaceImgAction(text:String)
}

class DelegateTestViewController: UIViewController {

    var delegate:FaceDelegate!
    
    init(coder aDecoder: NSCoder!) {
        super.init(coder:aDecoder)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func buttonAction(sender: AnyObject) {
        self.delegate.selectedFaceImgAction("testText")
    }
    
}

 

2,这里实现delegate方法

import UIKit


class ViewController: UIViewController ,FaceDelegate{
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
    }

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

    @IBAction func button1Action(sender: AnyObject) {
        var storyboard:UIStoryboard = UIStoryboard(name:"Main",bundle:nil)
        var viewController:DelegateTestViewController = storyboard.instantiateViewControllerWithIdentifier("DelegateTestViewController") as DelegateTestViewController
        self.view.addSubview(viewController.view)
        viewController.delegate = self
    }
    
    func selectedFaceImgAction(text:String){
        println(text)
    }

}

 

posted @ 2014-07-11 19:34  坤哥MartinLi  阅读(2145)  评论(0编辑  收藏  举报