Swift3.0-反向传值
1、使用代理协议
在ViewController.swift中
class ViewController: UIViewController,SubDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// setNavigation()
myButton()
}
func myButton() {
let btn = UIButton(type:UIButtonType.system) as UIButton
btn.frame = CGRect(x:60,y:100,width:100,height:30)
btn.backgroundColor = UIColor.lightGray
btn.setTitle("进入下一界面", for: UIControlState.normal)
self.view.addSubview(btn)
btn.addTarget(self, action: #selector(click), for: UIControlEvents.touchUpInside)
}
func click(){
let vc = SubViewController()
vc.delegate = self
// let vc = SubClosureViewController()
// vc.changeTitleAndClosure = {
// (title:String,color:UIColor) in
// self.title = title
// self.view.backgroundColor = color
// }
self.navigationController?.pushViewController(vc, animated: true)
}
func changeTitle(title: String) {
self.title = title
}
func changeColor(color: UIColor) {
self.view.backgroundColor = color
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
在
SubViewController.swift中
//在委托中定义协议
protocol SubDelegate:NSObjectProtocol {
//实现一些方法
func changeTitle(title:String)
func changeColor(color:UIColor)
}
class SubViewController: UIViewController {
//定义遵守协议
var delegate:SubDelegate?
override func viewDidLoad() {
super.viewDidLoad()
myView()
}
func myView() {
self.view.backgroundColor = UIColor.red
let tf = UITextField(frame:CGRect(x:60,y:180,width:100,height:30))
tf.borderStyle = UITextBorderStyle.roundedRect
tf.tag = 100
self.view.addSubview(tf)
let btn = UIButton(type:UIButtonType.system) as UIButton
btn.frame = CGRect(x:60,y:100,width:100,height:30)
btn.backgroundColor = UIColor.lightGray
btn.setTitle("返回上一界面", for: UIControlState.normal)
self.view.addSubview(btn)
btn.addTarget(self, action: #selector(click), for: UIControlEvents.touchUpInside)
}
func click(){
let tf = self.view.viewWithTag(100) as? UITextField
delegate?.changeTitle(title: tf!.text!)
delegate?.changeColor(color: UIColor.blue)
self.navigationController?.popViewController(animated: true)
}
}
2、使用闭包
在ViewController.swift中
class ViewController: UIViewController,SubDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// setNavigation()
myButton()
}
func myButton() {
let btn = UIButton(type:UIButtonType.system) as UIButton
btn.frame = CGRect(x:60,y:100,width:100,height:30)
btn.backgroundColor = UIColor.lightGray
btn.setTitle("进入下一界面", for: UIControlState.normal)
self.view.addSubview(btn)
btn.addTarget(self, action: #selector(click), for: UIControlEvents.touchUpInside)
}
func click(){
// let vc = SubViewController()
// vc.delegate = self
let vc = SubClosureViewController()
vc.changeTitleAndClosure = {
(title:String,color:UIColor) in
self.title = title
self.view.backgroundColor = color
}
self.navigationController?.pushViewController(vc, animated: true)
}
}
在
SubClosureViewController.swift中
class SubClosureViewController: UIViewController {
//定义一个闭包
var changeTitleAndClosure:((_ title:String,_ color:UIColor) -> Void)?
override func viewDidLoad() {
super.viewDidLoad()
myView()
}
func myView() {
self.view.backgroundColor = UIColor.red
let tf = UITextField(frame:CGRect(x:60,y:180,width:100,height:30))
tf.borderStyle = UITextBorderStyle.roundedRect
tf.tag = 100
self.view.addSubview(tf)
let btn = UIButton(type:UIButtonType.system) as UIButton
btn.frame = CGRect(x:60,y:100,width:100,height:30)
btn.backgroundColor = UIColor.lightGray
btn.setTitle("返回上一界面", for: UIControlState.normal)
self.view.addSubview(btn)
btn.addTarget(self, action: #selector(click), for: UIControlEvents.touchUpInside)
}
func click(){
let tf = self.view.viewWithTag(100) as? UITextField
changeTitleAndClosure?(tf!.text!,UIColor.green)
self.navigationController?.popViewController(animated: true)
}
}