转载
Swift使用delegate和closure进行传值:类似oc的代理和block
firstViewController.swift文件
import UIKit
class firstViewController:UIViewController,passValueDelegate {
var passValueLabel:UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor =UIColor.whiteColor()
let pushBtn =UIButton(type: .Custom)
pushBtn.frame =CGRectMake(100,150, kMainScreenWidth -200, 40)
pushBtn.setTitle("secondCon", forState: .Normal)
self.view.addSubview(pushBtn)
pushBtn.addTarget(self, action:Selector("pushTosecondViewController"), forControlEvents: .TouchUpInside)
pushBtn.setTitleColor(UIColor.redColor(), forState: .Normal)
passValueLabel =UILabel.init(frame:CGRectMake(20,100, kMainScreenWidth -40, 30))
passValueLabel.backgroundColor =UIColor.grayColor()
self.view.addSubview(passValueLabel)
// Do any additional setup after loading the view.
}
func pushTosecondViewController() {
let secondCon =secondViewController()
//利用闭包传值:跟oc的block很像
secondCon.myClosure = {
[unowned self](newValue:AnyObject) ->Void
in
self.passValueLabel.text = newValueas? String
}
secondCon.delegate =self
self.navigationController?.pushViewController(secondCon, animated:true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: - passValueDelegate代理回调
func passValue(newValue:AnyObject) {
print("接收代理回调的值:(newValue)")
passValueLabel.text = newValueas? String
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
secondViewController.swift文件
import UIKit
//利用代理传值
protocol passValueDelegate:NSObjectProtocol {
func passValue(newValue:AnyObject)
}
//闭包回调传值
typealias sendValueClosure = (newValue:AnyObject) ->Void
class secondViewController: UIViewController {
var myClosure:sendValueClosure?
weakvar delegate:passValueDelegate?
var textField:UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor =UIColor.whiteColor()
textField = UITextField.init(frame: CGRectMake(20, 100, kMainScreenWidth - 40, 30))
textField.backgroundColor =UIColor.yellowColor()
textField.placeholder ="输入newValue"
self.view.addSubview(textField)
let delegateBtn = UIButton(type: .Custom)
delegateBtn.frame = CGRectMake(20, 150, kMainScreenWidth/2.0 -40, 40)
delegateBtn.setTitleColor(UIColor.redColor(), forState: .Normal)
delegateBtn.setTitle("delegate传值", forState: .Normal)
self.view.addSubview(delegateBtn)
delegateBtn.addTarget(self, action:Selector("delegatePassValueBtnPressed"), forControlEvents: .TouchUpInside)
let closureBtn = UIButton(type: .Custom)
closureBtn.frame = CGRectMake(kMainScreenWidth/2.0+20,150, kMainScreenWidth/2.0 -40, 40)
closureBtn.setTitle("闭包传值", forState: .Normal)
closureBtn.setTitleColor(UIColor.redColor(), forState: .Normal)
self.view.addSubview(closureBtn)
closureBtn.addTarget(self, action:Selector("closurePassValueBtnPressed"), forControlEvents: .TouchUpInside)
// Do any additional setup after loading the view.
}
//MARK: - 代理传值
func delegatePassValueBtnPressed(){
delegate?.passValue(textField.text!)
self.navigationController?.popViewControllerAnimated(true)
}
//MARK: - 闭包传值
func closurePassValueBtnPressed(){
if myClosure !=nil {
myClosure!(newValue: textField.text!)
}
self.navigationController?.popViewControllerAnimated(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}