AppDelegate:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
//头一个的label与第二个tenfield之间传值,第二个texfild与第二个label传值
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.backgroundColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)
self.window?.makeKeyAndVisible()
//创建导航视图控制器的根视图
let vc = ViewController()
//2.创建导航视图控制器,并为她制定根视图控制器
let navigation = UINavigationController(rootViewController: vc)
//3.将导航视图控制器设置为window的根视图控制器
self.window?.rootViewController = navigation
return true
}
ViewController:
import UIKit
//代理传值第四步
class ViewController: UIViewController,SencondViewControllerDelegate {
//两个属性
var label:UILabel!
var textfield:UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.setupViews()
self.view.backgroundColor = #colorLiteral(red: 0.8549019694, green: 0.250980407, blue: 0.4784313738, alpha: 1)
self.title = "FirstVC"
}
func setupViews() {
self.label = UILabel(frame: CGRect(x: 107, y: 150, width: 200, height: 40))
self.label.backgroundColor = UIColor.blue
self.view.addSubview(label)
self.textfield = UITextField(frame: CGRect(x: 107, y: 220, width: 200, height: 40))
self.textfield.backgroundColor = UIColor.green
self.view.addSubview(textfield)
let button = UIButton(frame: CGRect(x: 107, y: 280, width: 200, height: 40))
button.setTitle("跳转", for: .normal)
button.backgroundColor = UIColor.cyan
self.view.addSubview(button)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
}
传值:
func buttonAction() {
let secondVC = SencondViewController()
//属性赋值第二步
secondVC.content = self.textfield.text
//闭包传值第二步
secondVC.passvalue = {
(text:String) in
//取出参数值为前一个界面的控件赋值
self.label.text = text
}
//代理传值的第三步
secondVC.delegate = self
self.navigationController?.pushViewController(secondVC, animated: true)
}
//代理传值的第五部
func sendValue(text: String) {
self.label.text = text
}
SencondViewController.swift:
//从前往后
//属性传值:分为1.定义一个要与要传数据类型相同的属性
// 2.给属性赋值
// 3.取出属性中的值使用
/*从后往前传值
闭包传值:
1.在后一个界面定义一个闭包属性
2.在前一个界面为闭包属性赋值
3.在pop回去之前调用闭包
代理传值
1.定义协议
2.定义代理属性
3.在外界指定代理对象
4.代理对象所在类遵循协议
5.代理对象所在类实现协议中的方法
6.通知代理对象干活
*/
import UIKit
//代理第一步
protocol SencondViewControllerDelegate {
func sendValue(text:String)
}
class SencondViewController: UIViewController {
//两个属性
var label:UILabel!
var textfield:UITextField!
//属性传值第一步:
var content: String!
//闭包传值第一步
var passvalue:((String)->())!
//代理传值第二步
var delegate:SencondViewControllerDelegate!
override func viewDidLoad() {
super.viewDidLoad()
self.setupViews()
//属性传值第三部
self.label.text = self.content
self.view.backgroundColor = #colorLiteral(red: 0.721568644, green: 0.8862745166, blue: 0.5921568871, alpha: 1)
self.title = "sencondVC"
}
func setupViews() {
self.label = UILabel(frame: CGRect(x: 107, y: 150, width: 200, height: 40))
self.label.backgroundColor = UIColor.blue
self.view.addSubview(label)
self.textfield = UITextField(frame: CGRect(x: 107, y: 220, width: 200, height: 40))
self.textfield.backgroundColor = UIColor.green
self.view.addSubview(textfield)
let button = UIButton(frame: CGRect(x: 107, y: 280, width: 200, height: 40))
button.setTitle("返回", for: .normal)
button.backgroundColor = UIColor.cyan
self.view.addSubview(button)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
}
func buttonAction() {
//闭包传值的第三部
// self.passvalue(self.textfield.text!)
//代理传值第六步
self.delegate.sendValue(text: self.textfield.text!)
self.navigationController?.popViewController(animated: true)
}