视图1代码:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor();
self.title = "视图1"
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let vc = NextViewController()
//回调的位置
vc.myCallBack = {(color) -> UIColor in
self.view.backgroundColor = color
return UIColor.greenColor()
}
self.navigationController?.pushViewController(vc, animated: true);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
视图2代码:
//
typealias CallBack = (str:UIColor)-> UIColor
import UIKit
class NextViewController: UIViewController {
var myCallBack : CallBack?
override func viewDidLoad() {
super.viewDidLoad()
self.title = "视图2"
self.view.backgroundColor = UIColor.whiteColor()
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if (myCallBack != nil) {
self.view.backgroundColor = myCallBack!(str:UIColor.redColor());
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}