1.属性传值,代理协议传值
创建视图控制区,导航视图控制器
在AppDelegate类下
func application()方法下
let vc = ViewController()
let navc = UINavigationController(rootViewController: vc)
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.rootViewController = navc
window?.makeKeyAndVisible()
ViewController类下
var labelStr:UILabel?//设置全局变量labelStr
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.whiteColor() //设置背景颜色
navigationItem.title = "first"//设置导航栏标题
labelStr = UILabel(frame: CGRectMake(100, 300, 100, 30))//设置labelStr的坐标,宽高
labelStr!.text = "海贼王"
labelStr!.textColor = UIColor.blackColor()
view.addSubview(labelStr!)
let nextBtn = UIButton(frame: CGRectMake(100, 400, 80, 30)) //设置按钮
nextBtn.setTitleColor(UIColor.redColor(), forState: .Normal)
nextBtn.setTitle("下一页", forState: UIControlState.Normal)
nextBtn.backgroundColor = UIColor.yellowColor()
nextBtn.addTarget(self, action: "leftBtn", forControlEvents: UIControlEvents.TouchUpInside)//设置nextBtn的点击事件
view.addSubview(nextBtn)
}
func leftBtn(){
let vc2 = ViewController2()
//属性传值
vc2.labelStr = labelStr!.text
//将当前控制器设置vc2的代理
vc2.delegate = self
navigationController?.pushViewController(vc2, animated: true)//跳转到ViewController2
}
//完成协议内容
extension ViewController:secondProtocol{
func translateStr(str: String) {
//将ViewController的label.text 设置为 ViewController2 中的str
labelStr?.text = str
}
}
在ViewController2类下
import UIKit
//从后往前传值的协议
protocol secondProtocol {
//需要传什么类型的参数,参数列表就写什么
func translateStr(str:String)
}
class ViewController2: UIViewController {
//定义代理属性
var delegate:secondProtocol?
//定义一个labelStr属性负责接收来自前一个页面传过来的值
var labelStr:String?
var textFeild:UITextField?
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Second"
view.backgroundColor = UIColor.grayColor()
//设置文本框
textFeild! = UITextField(frame: CGRectMake(100, 300, 180, 30))
textFeild!.placeholder = "请输入文字"
textFeild?.text = labelStr
view.addSubview(textFeild!)
//设置按钮
let nextBtn = UIButton(frame: CGRectMake(100, 400, 80, 30))
nextBtn.setTitle("上一页", forState: UIControlState.Normal)
nextBtn.setTitleColor(UIColor.redColor(), forState: .Normal)
nextBtn.backgroundColor = UIColor.yellowColor()
nextBtn.addTarget(self, action: "rightBtn", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(nextBtn)
}
func rightBtn(){
//执行协议
delegate?.translateStr((textFeild?.text!)!)
navigationController?.popViewControllerAnimated(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
2.闭包传值,用闭包传值改变上一页的背景颜色
ViewController类下
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "first"
view.backgroundColor = UIColor.whiteColor()
let btn = UIButton(frame: CGRectMake(100, 100, 100, 50))
btn.layer.borderWidth = 2
btn.setTitle("Push", forState: .Normal)
btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
btn.addTarget(self, action: "btn", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(btn)
}
func btn(){
let vc2 = ViewController2()
//闭包的实现
vc2.color = {
(str:UIColor) -> Void
in
self.view.backgroundColor = str
}
navigationController?.pushViewController(vc2, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
import UIKit
class ViewController2: UIViewController {
var color:((str:UIColor)->Void)?//设置闭包
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Second"
view.backgroundColor = UIColor.whiteColor()
let btn = UIButton(frame: CGRectMake(100, 100, 100, 50))
btn.setTitle("Pop", forState: .Normal)
btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
btn.layer.borderWidth = 2
btn.addTarget(self, action: "btn", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(btn)
}
func btn(){
let str = UIColor.blueColor()//设置str为蓝色
//调用闭包
color!(str: str)
navigationController?.popViewControllerAnimated(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}