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()
//創(chuàng)建導航視圖控制器的根視圖
let vc = ViewController()
//2.創(chuàng)建導航視圖控制器梗醇,并為她制定根視圖控制器
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("跳轉(zhuǎn)", 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
//取出參數(shù)值為前一個界面的控件賦值
self.label.text = text
}
//代理傳值的第三步
secondVC.delegate = self
self.navigationController?.pushViewController(secondVC, animated: true)
}
//代理傳值的第五部
func sendValue(text: String) {
self.label.text = text
}
SencondViewController.swift:
//從前往后
//屬性傳值:分為1.定義一個要與要傳數(shù)據(jù)類型相同的屬性
// 2.給屬性賦值
// 3.取出屬性中的值使用
/*從后往前傳值
閉包傳值:
1.在后一個界面定義一個閉包屬性
2.在前一個界面為閉包屬性賦值
3.在pop回去之前調(diào)用閉包
代理傳值
1.定義協(xié)議
2.定義代理屬性
3.在外界指定代理對象
4.代理對象所在類遵循協(xié)議
5.代理對象所在類實現(xiàn)協(xié)議中的方法
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)
}