swift代理delegate

实现代理传值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import UIKit

class ViewController: UIViewController,ChangeValueDelegate{

var btn = UIButton()

override func viewDidLoad() {
super.viewDidLoad()

self.view.backgroundColor = UIColor.whiteColor()

btn.setTitle("Change", forState:.Normal)
btn.backgroundColor = UIColor.blueColor()
btn.frame = CGRect(x: 100, y: 100, width: 100, height: 50)
btn.addTarget(self, action: #selector(change), forControlEvents: .TouchUpInside)
self.view.addSubview(btn)

}

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

// delegate 代理方法
func changeValue(value: String) {

btn.setTitle(value, forState: .Normal)
print("value:\(value)")

}
// 传值
func change(){

let vc = ChangeValueViewController()
vc.delegate = self
self.navigationController?.pushViewController(vc, animated: true)

}

}

协议

1
2
3
4
5
protocol ChangeValueDelegate {

func changeValue(value:String)

}

使用代理传值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class ChangeValueViewController: UIViewController {

var delegate:ChangeValueDelegate?

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.whiteColor()

self.delegate?.changeValue("ok")
}

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