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