頁面?zhèn)髦凳侵福焊缸禹撁嬷g蛔六、非父子頁面之間敏储、兄弟頁面之間旺订、非兄弟頁面之間數(shù)據(jù)互通的方式,是為頁面?zhèn)髦担▊€人見解)
這里只講怎么使用抵皱,至于每個傳值方式是什么意思帖汞,什么原理吧啦吧啦的一堆問題,請移步百度找到你想要的答案砾肺,謝謝挽霉!
這里我所整理的傳值方式有六個,分別是:
1变汪、單例傳值
2侠坎、通知傳值
3、屬性傳值
4裙盾、閉包傳值(Block)
5实胸、代理傳值
6、NSUserDefaults傳值
1番官、單例傳值
注:這里就不寫怎么創(chuàng)建單例類了庐完,你實在是想要知道只能去百度了,謝謝徘熔!
言歸正傳
傳值描述:
A頁面假褪、B頁面、X單例類
A頁面-》B頁面-》B頁面給X單例類屬性賦值-》A頁面從X單例類屬性獲取所傳的值
實現(xiàn)代碼步驟:
1.先創(chuàng)建一個單例類近顷,聲明好所需的屬性
2.在B頁面調(diào)用單例類的屬性生音,并給單例類屬性賦值
3.在A頁面中使用 viewWillAppear() 生命周期里,使用單例類屬性窒升,給UI控件賦值
直接上代碼
單例類代碼
import UIKit
//MARK:- X單例類
class HYSingle: NSObject {
static let sharedInstance = HYSingle()
private override init() {}
//聲明字符串變量
var sendValue = ""
}
賦值頁面 - B頁面
import UIKit
//MARK:- B頁面
class ADLViewController: UIViewController {
//傳值的輸入框
var textF = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
//傳值的輸入框
textF.frame = CGRect(x: 50, y: 170, width: view.frame.size.width - 100, height: 50)
textF.attributedPlaceholder = NSAttributedString(string: "請輸入要傳的值", attributes: [NSAttributedString.Key.foregroundColor : UIColor.init(red: 1.000, green: 1.000, blue: 1.000, alpha: 0.5)])
textF.borderStyle = .line
textF.textColor = .white
textF.backgroundColor = UIColor.init(red: 0.283, green: 0.290, blue: 0.293, alpha: 1)
view.addSubview(textF)
//發(fā)送傳值按鈕
let btn = UIButton()
btn.frame = CGRect(x: 70, y: (textF.frame.origin.y+textF.frame.size.height)+50, width: view.bounds.width-(70*2), height: 50)
btn.backgroundColor = .orange
btn.setTitle("開始傳值了", for: .normal)
btn.addTarget(self, action: #selector(btnAction(btn:)), for: .touchUpInside)
view.addSubview(btn)
}
/******************向A頁面?zhèn)髦?*************************/
@objc func btnAction(btn:UIButton){
//向A頁面?zhèn)髦? //獲取輸入框內(nèi)容缀遍,并給單例類屬性賦值
HYSingle.sharedInstance.sendValue = textF.text!
//返回上級頁面
self.navigationController?.popViewController(animated: true)
}
/********************************************/
}
獲取頁面 - A頁面
import UIKit
//MARK:- A頁面
class HDLViewController: UIViewController {
var labels = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
//跳轉(zhuǎn)下級頁面
let ymBtn_01 = UIButton()
ymBtn_01.frame = CGRect(x: 50, y: 130, width: 120, height: 50)
ymBtn_01.setTitle("跳轉(zhuǎn)下級頁面", for: .normal)
ymBtn_01.backgroundColor = .brown
ymBtn_01.tag = 10001
ymBtn_01.addTarget(self, action: #selector(tiaozhuanyemianclick(sender:)), for: .touchUpInside)
view.addSubview(ymBtn_01)
//接收傳值的標(biāo)簽
labels.frame = CGRect(x: 50, y: (ymBtn_01.frame.origin.y+ymBtn_01.frame.size.height)+50, width: view.frame.size.width-100, height: 50)
labels.backgroundColor = .darkGray
labels.textColor = .white
view.addSubview(labels)
}
/******************接收B頁面?zhèn)鱽淼闹?*************************/
//接收B頁面?zhèn)鱽淼闹? override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//接收傳值,并賦值給標(biāo)簽
self.labels.text = HYSingle.sharedInstance.sendValue
}
/********************************************/
@objc func tiaozhuanyemianclick(sender:UIButton){
//聲明變量名饱须,并初始化類名
let aDLVC = ADLViewController.init()
aDLVC.title = "頁面 2"
aDLVC.view.backgroundColor = UIColor.init(named: "bgColor")
//跳轉(zhuǎn)下級頁面
navigationController?.pushViewController(aDLVC, animated: true)
}
}
----------------------------------分割線---------------------------------------
2. 通知傳值
傳值描述:
A頁面域醇、B頁面
A頁面-》B頁面-》B頁面使用通知(NotificationCenter)傳值-》A頁面使用通知(NotificationCenter)獲取傳值
實現(xiàn)代碼步驟:
1.在B頁面中使用通知中心(NotificationCenter)調(diào)用post去賦值,同時發(fā)送通知消息
2.在B頁面中需要實現(xiàn)“移除通知”,必須實現(xiàn)這個代碼
3.在A頁面中使用通知中心(NotificationCenter)調(diào)用addObserver來設(shè)置通知的監(jiān)聽事件
4.實現(xiàn)通知中心(NotificationCenter)的監(jiān)聽事件譬挚,并在方法里獲取B頁面?zhèn)鬟^來的值
注:NSNotification.Name(rawValue: "NotificationTest") 锅铅,在A、B頁面中一定要一致减宣,否則會無法接收傳值
直接上代碼
A頁面代碼:
import UIKit
class HTZViewController: UIViewController {
//聲明全局變量名
var textLabel_1 = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
//聲明變量盐须,并初始化
let ymBtn_01 = UIButton()
ymBtn_01.frame = CGRect(x: 50, y: 130, width: 220, height: 50)
ymBtn_01.setTitle("跳轉(zhuǎn)發(fā)送通知頁面", for: .normal)
ymBtn_01.backgroundColor = .brown
ymBtn_01.tag = 10001
ymBtn_01.addTarget(self, action: #selector(tiaozhuanyemianclick(sender:)), for: .touchUpInside)
view.addSubview(ymBtn_01)
//聲明變量,并初始化
let textLabel = UILabel()
textLabel.font = .systemFont(ofSize: 20)
textLabel.frame = CGRect(x: 50, y: ymBtn_01.frame.origin.y+150, width: 100, height: 50)
textLabel.text = "接收傳值:"
view.addSubview(textLabel)
//接收傳值的標(biāo)簽
textLabel_1.backgroundColor = .darkGray
textLabel_1.font = .systemFont(ofSize: 20)
textLabel_1.frame = CGRect(x: (textLabel.frame.origin.x+textLabel.frame.size.width)+20, y: ymBtn_01.frame.origin.y+150, width: 200, height: 50)
textLabel_1.text = "空值"
textLabel_1.textColor = .white
textLabel_1.numberOfLines = 0
textLabel_1.lineBreakMode = .byWordWrapping
view.addSubview(textLabel_1)
}
@objc func tiaozhuanyemianclick(sender:UIButton){
let aTZVC = ATZViewController.init()
aTZVC.title = "發(fā)送通知頁面"
aTZVC.view.backgroundColor = UIColor.init(named: "bgColor")
/*
創(chuàng)建通知中心
設(shè)置監(jiān)聽方法
設(shè)置通知的名字
*/
NotificationCenter.default.addObserver(self, selector: #selector(login(_notification:)), name: NSNotification.Name(rawValue: "NotificationTest"), object: nil)
//模態(tài)彈出
self.present(aTZVC, animated: true, completion: nil)
}
//實現(xiàn)通知的監(jiān)聽方法漆腌,并在里面做好接收傳值
@objc private func login(_notification:Notification){
//聲明變量贼邓,并把獲取的值賦值給變量
let str = _notification.userInfo!["post"]
//通過變量賦值給接收傳值的標(biāo)簽
textLabel_1.text = str as? String
print("接收傳值:",str!)
}
}
B頁面代碼:
import UIKit
class ATZViewController: UIViewController {
var textF = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
let ymBtn_01 = UIButton()
ymBtn_01.frame = CGRect(x: 50, y: 130, width: 220, height: 50)
ymBtn_01.setTitle("發(fā)送通知,并返回傳值", for: .normal)
ymBtn_01.backgroundColor = .brown
ymBtn_01.tag = 10001
ymBtn_01.addTarget(self, action: #selector(tiaozhuanyemianclick(sender:)), for: .touchUpInside)
view.addSubview(ymBtn_01)
textF.frame = CGRect(x: 50, y: (ymBtn_01.frame.origin.y + ymBtn_01.frame.size.height) + 50, width: view.frame.size.width - 100, height: 50)
textF.attributedPlaceholder = NSAttributedString(string: "請輸入要傳的值", attributes: [NSAttributedString.Key.foregroundColor : UIColor.yellow])
textF.borderStyle = .line
textF.textColor = .white
textF.backgroundColor = .darkGray
view.addSubview(textF)
}
//按鈕的監(jiān)聽方法
@objc func tiaozhuanyemianclick(sender:UIButton){
//發(fā)送通知
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "NotificationTest"), object:self ,userInfo: ["post":textF.text!])
//退出模態(tài)窗口
self.dismiss(animated: true, completion: nil)
}
//最后這里一定要記得移除通知
deinit {
print("移除通知")
NotificationCenter.default.removeObserver(self)
}
}
----------------------------------分割線---------------------------------------
3. 屬性傳值
傳值描述:
A頁面闷尿、B頁面
A頁面在跳轉(zhuǎn)的同時通過B頁面聲明全局變量(可以是數(shù)組塑径、字典)傳值給B頁面 -》B頁面聲明全局變量(可以是數(shù)組、字典)填具,并接收A頁面?zhèn)鱽淼闹?/p>
實現(xiàn)代碼步驟:
1.在B頁面中聲明全局變量统舀,以便在其他頁面中調(diào)用,同時使用全局變量賦值給UI控件
2.在A頁面中跳轉(zhuǎn)之前劳景,A頁面所傳的值通過賦值給B頁面屬性進(jìn)行傳值
注:屬性傳值代碼中有使用String(字符串)誉简、Array(數(shù)組)、實例方法傳值枢泰,這需要自己仔細(xì)看代碼描融,梳理好流程再去寫代碼
直接上代碼
A頁面代碼:
import UIKit
class HSXViewController: UIViewController {
var textF = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
//傳值按鈕
let ymBtn_01 = UIButton()
ymBtn_01.frame = CGRect(x: 50, y: 130, width: 220, height: 50)
ymBtn_01.setTitle("跳轉(zhuǎn)發(fā)送傳值頁面", for: .normal)
ymBtn_01.backgroundColor = .brown
ymBtn_01.tag = 10001
ymBtn_01.addTarget(self, action: #selector(tiaozhuanyemianclick(sender:)), for: .touchUpInside)
view.addSubview(ymBtn_01)
//傳值的輸入框
textF.frame = CGRect(x: 50, y: (ymBtn_01.frame.origin.y + ymBtn_01.frame.size.height) + 50, width: view.frame.size.width - 100, height: 50)
textF.attributedPlaceholder = NSAttributedString(string: "請輸入要傳的值", attributes: [NSAttributedString.Key.foregroundColor : UIColor.yellow])
textF.borderStyle = .line
textF.textColor = .white
textF.backgroundColor = .darkGray
view.addSubview(textF)
}
//實現(xiàn)傳值按鈕的監(jiān)聽方法
@objc func tiaozhuanyemianclick(sender:UIButton){
let aSXVC = ASXViewController()
aSXVC.myStr = textF.text!
aSXVC.view.backgroundColor = .white
//屬性傳值 - string
aSXVC.title = "接收屬性傳值"
/*
如果不在輸入框內(nèi)填寫想要傳的值铝噩,那么下面的數(shù)組也可以實現(xiàn)屬性傳值
數(shù)組衡蚂,不會顯示在頁面上,但可以在下面控制臺里打印出來
*/
//屬性傳值 - Array
let arrs:Array = ["寶馬", "奔馳", "奧迪", "蘭博基尼", "凱迪拉克", "法拉利"]
//拷貝數(shù)組
for item in arrs {
aSXVC.arr.append(item)
}
/*
屬性傳值 - 實例方法
調(diào)用:在上級界面中骏庸,通過class名去調(diào)內(nèi)部方法名
class.方法名()
*/
aSXVC.counter.increment(amount: 10203020301005060)
//開始跳轉(zhuǎn)界面
self.navigationController?.pushViewController(aSXVC, animated: true)
}
}
B頁面代碼:
import UIKit
class ASXViewController: UIViewController {
//屬性傳值 - string
var myStr = String()
//屬性傳值 - array
var arr:[String] = []
//屬性傳值 - 全局引用實例方法 - 1
var counter = Counter()
override func viewDidLoad() {
super.viewDidLoad()
//返回上級界面的按鈕
let ymBtn_01 = UIButton()
ymBtn_01.frame = CGRect(x: 50, y: 130, width: 220, height: 50)
ymBtn_01.setTitle("返回上個頁面", for: .normal)
ymBtn_01.backgroundColor = .brown
ymBtn_01.tag = 10001
ymBtn_01.addTarget(self, action: #selector(tiaozhuanyemianclick(sender:)), for: .touchUpInside)
view.addSubview(ymBtn_01)
//接收傳值的標(biāo)簽
let textLabel = UILabel()
textLabel.font = .systemFont(ofSize: 20)
textLabel.frame = CGRect(x: 50, y: ymBtn_01.frame.origin.y+150, width: view.frame.size.width - 100, height: 50)
textLabel.text = myStr
textLabel.numberOfLines = 0
textLabel.lineBreakMode = .byWordWrapping
textLabel.backgroundColor = .darkGray
textLabel.textColor = .white
view.addSubview(textLabel)
}
// 視圖將要顯示時調(diào)用該方法
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("視圖即將顯示")
/*
由于viewDidLoad()無法接收傳值毛甲,所以只能在視圖即將顯示的時候,來接收上級界面所傳的值
*/
//打印接收的數(shù)組
for item in self.arr {
//打印接收數(shù)組的值
print(item)
}
}
//實現(xiàn)按鈕的監(jiān)聽方法
@objc func tiaozhuanyemianclick(sender:UIButton){
//開始返回上級界面
self.navigationController?.popViewController(animated: true)
}
}
// MARK: - 全局引用實例方法 - 2
/*
通過實例方法也可以做到屬性傳值
*/
class Counter {
func increment(amount:Int) {
print("===amount:",amount)
}
}
----------------------------------分割線---------------------------------------
4. 閉包傳值(Block)
傳值描述:
A頁面具被、B頁面
A頁面-》B頁面-》B頁面聲明閉包(Block)玻募,并在返回A頁面的同時使用閉包傳值(Block)-》A頁面獲取傳值
實現(xiàn)代碼步驟:
1.在B頁面中聲明全局的閉包(Block),并設(shè)置好要參數(shù)類型
2.在B頁面中使用閉包(Block)賦值給閉包的參數(shù)
3.在A頁面中在跳轉(zhuǎn)頁面之前一姿,使用B頁面的閉包給A頁面的標(biāo)簽賦值
直接上代碼
A頁面代碼:
import UIKit
class HBBViewController: UIViewController {
//聲明全局變量七咧,并初始化
var textLabel = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
//傳值按鈕
let ymBtn_01 = UIButton()
ymBtn_01.frame = CGRect(x: 50, y: 130, width: view.frame.size.width - 100, height: 50)
ymBtn_01.setTitle("跳轉(zhuǎn)下級頁面", for: .normal)
ymBtn_01.backgroundColor = .brown
ymBtn_01.tag = 10001
ymBtn_01.addTarget(self, action: #selector(tiaozhuanyemianclick(sender:)), for: .touchUpInside)
view.addSubview(ymBtn_01)
//接收傳值的標(biāo)簽
textLabel.font = .systemFont(ofSize: 20)
textLabel.frame = CGRect(x: 50, y: ymBtn_01.frame.origin.y+150, width: view.frame.size.width - 100, height: 50)
textLabel.text = "空值"
textLabel.numberOfLines = 0
textLabel.lineBreakMode = .byWordWrapping
textLabel.backgroundColor = .darkGray
textLabel.textColor = UIColor.init(red: 1.000, green: 1.000, blue: 1.000, alpha: 0.6)
view.addSubview(textLabel)
//提示標(biāo)簽
let textLabel_1 = UILabel()
textLabel_1.font = .systemFont(ofSize: 20)
textLabel_1.frame = CGRect(x: 50, y: (textLabel.frame.origin.y+textLabel.frame.size.height)+50, width: view.frame.size.width - 100, height: 50)
textLabel_1.text = "請先到下級頁面中填寫要傳回的值"
textLabel_1.numberOfLines = 0
textLabel_1.lineBreakMode = .byWordWrapping
textLabel_1.backgroundColor = .darkGray
textLabel_1.textColor = .yellow
textLabel_1.font = .systemFont(ofSize: 15)
textLabel_1.textAlignment = .center
view.addSubview(textLabel_1)
}
//實現(xiàn)傳值按鈕的監(jiān)聽方法
@objc func tiaozhuanyemianclick(sender:UIButton){
let aBBVC = ABBViewController()
aBBVC.view.backgroundColor = .white
aBBVC.title = "傳值界面"
//調(diào)用閉包獲取傳值,并賦值給標(biāo)簽
aBBVC.bbchange = {
(title:String) in
//賦值給標(biāo)簽
self.textLabel.text = title
print("接收傳值:",title)
}
self.navigationController?.pushViewController(aBBVC, animated: true)
}
}
B頁面代碼:
import UIKit
class ABBViewController: UIViewController {
//聲明全局閉包(Block)
var bbchange:((_ title:String)->Void)?
//聲明全局變量叮叹,并初始化
var textF = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
//傳值按鈕
let ymBtn_01 = UIButton()
ymBtn_01.frame = CGRect(x: 50, y: 130, width: view.frame.size.width - 100, height: 50)
ymBtn_01.setTitle("跳轉(zhuǎn)到上級頁面艾栋,并傳回值", for: .normal)
ymBtn_01.backgroundColor = .brown
ymBtn_01.tag = 10001
ymBtn_01.addTarget(self, action: #selector(tiaozhuanyemianclick(sender:)), for: .touchUpInside)
view.addSubview(ymBtn_01)
//傳值的輸入框
textF.frame = CGRect(x: 50, y: (ymBtn_01.frame.origin.y + ymBtn_01.frame.size.height) + 50, width: view.frame.size.width - 100, height: 50)
textF.attributedPlaceholder = NSAttributedString(string: "請輸入要傳的值", attributes: [NSAttributedString.Key.foregroundColor : UIColor.init(red: 1.000, green: 1.000, blue: 1.000, alpha: 0.5)])
textF.borderStyle = .line
textF.textColor = .white
textF.backgroundColor = UIColor.init(red: 0.283, green: 0.290, blue: 0.293, alpha: 1)
view.addSubview(textF)
//提示標(biāo)簽
let textLabel_1 = UILabel()
textLabel_1.font = .systemFont(ofSize: 20)
textLabel_1.frame = CGRect(x: 50, y: (textF.frame.origin.y+textF.frame.size.height)+50, width: view.frame.size.width - 100, height: 50)
textLabel_1.text = "請先上面的輸入框中填寫要傳回的值"
textLabel_1.numberOfLines = 0
textLabel_1.lineBreakMode = .byWordWrapping
textLabel_1.backgroundColor = UIColor.init(red: 0.238, green: 0.240, blue: 0.242, alpha: 0.5)
textLabel_1.textColor = .yellow
textLabel_1.font = .systemFont(ofSize: 15)
textLabel_1.textAlignment = .center
view.addSubview(textLabel_1)
}
//實現(xiàn)傳值按鈕的監(jiān)聽方法
@objc func tiaozhuanyemianclick(sender:UIButton){
bbchange?(textF.text!)
self.navigationController?.popViewController(animated: true)
}
}
----------------------------------分割線---------------------------------------
5. 代理傳值
傳值描述:
A頁面、B頁面
A頁面-》B頁面-》B頁面通過代理協(xié)議-》A頁面
實現(xiàn)代碼步驟:
1.在B頁面中聲明代理協(xié)議
2.在B頁面中實現(xiàn)代理協(xié)議
3.在B頁面中返回上級頁面之前蛉顽,使用代理協(xié)議賦值給參數(shù)
4.在A頁面中跳轉(zhuǎn)下級頁面之前蝗砾,設(shè)置B頁面的代理協(xié)議
5.在A頁面中實現(xiàn)B頁面的代理協(xié)議,并在代理協(xié)議中通過參數(shù)獲取傳值,然后賦值給UI控件
直接上代碼
A頁面代碼:
import UIKit
//添加代理協(xié)議
class HYDLViewController: UIViewController, HYDelegate {
//實現(xiàn)代理方法
func postValueToUpPage(str: String) {
//獲取值悼粮,并賦值給標(biāo)簽
textLabel.text = str
}
//聲明全局變量
var textLabel = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
//傳值按鈕
let ymBtn_01 = UIButton()
ymBtn_01.frame = CGRect(x: 50, y: 130, width: view.frame.size.width - 100, height: 50)
ymBtn_01.setTitle("跳轉(zhuǎn)下級頁面", for: .normal)
ymBtn_01.backgroundColor = .brown
ymBtn_01.tag = 10001
ymBtn_01.addTarget(self, action: #selector(tiaozhuanyemianclick(sender:)), for: .touchUpInside)
view.addSubview(ymBtn_01)
//接收傳值的標(biāo)簽
textLabel.font = .systemFont(ofSize: 20)
textLabel.frame = CGRect(x: 50, y: ymBtn_01.frame.origin.y+150, width: view.frame.size.width - 100, height: 50)
textLabel.text = "空值"
textLabel.numberOfLines = 0
textLabel.lineBreakMode = .byWordWrapping
textLabel.backgroundColor = .darkGray
textLabel.textColor = UIColor.init(red: 1.000, green: 1.000, blue: 1.000, alpha: 0.6)
view.addSubview(textLabel)
//提示標(biāo)簽
let textLabel_1 = UILabel()
textLabel_1.font = .systemFont(ofSize: 20)
textLabel_1.frame = CGRect(x: 50, y: (textLabel.frame.origin.y+textLabel.frame.size.height)+50, width: view.frame.size.width - 100, height: 50)
textLabel_1.text = "請先到下級頁面中填寫要傳回的值"
textLabel_1.numberOfLines = 0
textLabel_1.lineBreakMode = .byWordWrapping
textLabel_1.backgroundColor = .darkGray
textLabel_1.textColor = .yellow
textLabel_1.font = .systemFont(ofSize: 15)
textLabel_1.textAlignment = .center
view.addSubview(textLabel_1)
}
//實現(xiàn)傳值按鈕的監(jiān)聽方法
@objc func tiaozhuanyemianclick(sender:UIButton){
//聲明變量闲勺,并初始化類名
let aYDLVC = AYDLViewController()
//設(shè)置背景色,白色
aYDLVC.view.backgroundColor = .white
//設(shè)置導(dǎo)航標(biāo)題
aYDLVC.title = "傳值界面"
//設(shè)置代理協(xié)議扣猫,并遵守代理協(xié)議
aYDLVC.delegate = self
//跳轉(zhuǎn)下級頁面
self.navigationController?.pushViewController(aYDLVC, animated: true)
}
}
B頁面代碼:
import UIKit
//實現(xiàn)代理協(xié)議
@objc protocol HYDelegate:NSObjectProtocol {
//實現(xiàn)代理協(xié)議方法
func postValueToUpPage(str:String)
}
//MARK: - 分割線
class AYDLViewController: UIViewController {
//聲明代理變量名菜循,
weak var delegate:HYDelegate?
//聲明變量名,并初始化
var textF = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
//傳值按鈕
let ymBtn_01 = UIButton()
ymBtn_01.frame = CGRect(x: 50, y: 130, width: view.frame.size.width - 100, height: 50)
ymBtn_01.setTitle("跳轉(zhuǎn)到上級頁面苞笨,并傳回值", for: .normal)
ymBtn_01.backgroundColor = .brown
ymBtn_01.tag = 10001
ymBtn_01.addTarget(self, action: #selector(tiaozhuanyemianclick(sender:)), for: .touchUpInside)
view.addSubview(ymBtn_01)
//傳值的輸入框
textF.frame = CGRect(x: 50, y: (ymBtn_01.frame.origin.y + ymBtn_01.frame.size.height) + 50, width: view.frame.size.width - 100, height: 50)
textF.attributedPlaceholder = NSAttributedString(string: "請輸入要傳的值", attributes: [NSAttributedString.Key.foregroundColor : UIColor.init(red: 1.000, green: 1.000, blue: 1.000, alpha: 0.5)])
textF.borderStyle = .line
textF.textColor = .white
textF.backgroundColor = UIColor.init(red: 0.283, green: 0.290, blue: 0.293, alpha: 1)
view.addSubview(textF)
let textLabel_1 = UILabel()
textLabel_1.font = .systemFont(ofSize: 20)
textLabel_1.frame = CGRect(x: 50, y: (textF.frame.origin.y+textF.frame.size.height)+50, width: view.frame.size.width - 100, height: 50)
textLabel_1.text = "請先上面的輸入框中填寫要傳回的值"
textLabel_1.numberOfLines = 0
textLabel_1.lineBreakMode = .byWordWrapping
textLabel_1.backgroundColor = UIColor.init(red: 0.238, green: 0.240, blue: 0.242, alpha: 0.5)
textLabel_1.textColor = .yellow
textLabel_1.font = .systemFont(ofSize: 15)
textLabel_1.textAlignment = .center
view.addSubview(textLabel_1)
}
//MARK: - 分割線
//實現(xiàn)傳值按鈕的監(jiān)聽方法
@objc func tiaozhuanyemianclick(sender:UIButton){
delegate?.postValueToUpPage(str: textF.text!)
self.navigationController?.popViewController(animated: true)
}
}
----------------------------------分割線---------------------------------------
6. NSUserDefaults傳值
傳值描述:
A頁面债朵、B頁面
A頁面-》B頁面-》B頁面使用UserDefaults類存儲需要傳的值-》A頁面接收
實現(xiàn)代碼步驟:
1.在B頁面中返回上級頁面之前,通過UserDefaults來存儲需要傳的值
2.在A頁面中的生命周期 viewWillAppear() 里瀑凝,通過 UserDefaults 內(nèi)存的key來獲取值
注:UserDefaults里存有String(字符串)序芦、Array(數(shù)組)、Dictionary(字典)粤咪,這里需要注意的是在獲取值的時候谚中,如果要獲取數(shù)組,那么使用時是UserDefaults().array() 寥枝。如果是字典宪塔,那么使用時是UserDefaults(). dictionary() 。
直接上代碼
A頁面代碼:
import UIKit
class UserDefaultsViewController: UIViewController {
//聲明全局變量
var textLabel = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
//傳值按鈕
let ymBtn_01 = UIButton()
ymBtn_01.frame = CGRect(x: 50, y: 130, width: view.frame.size.width - 100, height: 50)
ymBtn_01.setTitle("跳轉(zhuǎn)下級頁面", for: .normal)
ymBtn_01.backgroundColor = .brown
ymBtn_01.tag = 10001
ymBtn_01.addTarget(self, action: #selector(tiaozhuanyemianclick(sender:)), for: .touchUpInside)
view.addSubview(ymBtn_01)
//接收傳值的標(biāo)簽
textLabel.font = .systemFont(ofSize: 20)
textLabel.frame = CGRect(x: 50, y: ymBtn_01.frame.origin.y+150, width: view.frame.size.width - 100, height: 50)
textLabel.text = "空值"
textLabel.numberOfLines = 0
textLabel.lineBreakMode = .byWordWrapping
textLabel.backgroundColor = .darkGray
textLabel.textColor = UIColor.init(red: 1.000, green: 1.000, blue: 1.000, alpha: 0.6)
view.addSubview(textLabel)
//提示標(biāo)簽
let textLabel_1 = UILabel()
textLabel_1.font = .systemFont(ofSize: 20)
textLabel_1.frame = CGRect(x: 50, y: (textLabel.frame.origin.y+textLabel.frame.size.height)+50, width: view.frame.size.width - 100, height: 50)
textLabel_1.text = "請先到下級頁面中填寫要傳回的值"
textLabel_1.numberOfLines = 0
textLabel_1.lineBreakMode = .byWordWrapping
textLabel_1.backgroundColor = .darkGray
textLabel_1.textColor = .yellow
textLabel_1.font = .systemFont(ofSize: 15)
textLabel_1.textAlignment = .center
view.addSubview(textLabel_1)
}
//視圖即將顯示
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
/*------------------------------*/
/*
Swift之UserDefaults數(shù)據(jù)存儲 http://www.reibang.com/p/99660cadfe16
1囊拜、存儲各種類型的數(shù)據(jù)
2某筐、獲取各種類型的存儲數(shù)據(jù)
*/
//獲取數(shù)組,就需要使用 xxx.array(key)
let strArray = UserDefaults().array(forKey: "userNameArray")
print("====array:",strArray as Any)
//獲取數(shù)組冠跷,就需要使用 xxx.dictionary(key)
let strDic = UserDefaults().dictionary(forKey: "userNameDictinary")
print("====Dictinary:",strDic as Any)
//聲明變量南誊,獲取userdefaults中的值,并賦值
let str = UserDefaults().string(forKey: "userName")
//賦值給標(biāo)簽
textLabel.text = str
}
//實現(xiàn)傳值按鈕的監(jiān)聽方法
@objc func tiaozhuanyemianclick(sender:UIButton){
//聲明變量蜜托,并初始化類名
let aUDVC = AUDViewController()
//設(shè)置背景色抄囚,白色
aUDVC.view.backgroundColor = .white
//設(shè)置導(dǎo)航標(biāo)題
aUDVC.title = "傳值界面"
//跳轉(zhuǎn)下級頁面
self.navigationController?.pushViewController(aUDVC, animated: true)
}
}
B頁面代碼:
import UIKit
class AUDViewController: UIViewController {
//聲明變量名,并初始化
var textF = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
//傳值按鈕
let ymBtn_01 = UIButton()
ymBtn_01.frame = CGRect(x: 50, y: 130, width: view.frame.size.width - 100, height: 50)
ymBtn_01.setTitle("跳轉(zhuǎn)到上級頁面橄务,并傳回值", for: .normal)
ymBtn_01.backgroundColor = .brown
ymBtn_01.tag = 10001
ymBtn_01.addTarget(self, action: #selector(tiaozhuanyemianclick(sender:)), for: .touchUpInside)
view.addSubview(ymBtn_01)
//傳值的輸入框
textF.frame = CGRect(x: 50, y: (ymBtn_01.frame.origin.y + ymBtn_01.frame.size.height) + 50, width: view.frame.size.width - 100, height: 50)
textF.attributedPlaceholder = NSAttributedString(string: "請輸入要傳的值", attributes: [NSAttributedString.Key.foregroundColor : UIColor.init(red: 1.000, green: 1.000, blue: 1.000, alpha: 0.5)])
textF.borderStyle = .line
textF.textColor = .white
textF.backgroundColor = UIColor.init(red: 0.283, green: 0.290, blue: 0.293, alpha: 1)
view.addSubview(textF)
let textLabel_1 = UILabel()
textLabel_1.font = .systemFont(ofSize: 20)
textLabel_1.frame = CGRect(x: 50, y: (textF.frame.origin.y+textF.frame.size.height)+50, width: view.frame.size.width - 100, height: 50)
textLabel_1.text = "請先上面的輸入框中填寫要傳回的值"
textLabel_1.numberOfLines = 0
textLabel_1.lineBreakMode = .byWordWrapping
textLabel_1.backgroundColor = UIColor.init(red: 0.238, green: 0.240, blue: 0.242, alpha: 0.5)
textLabel_1.textColor = .yellow
textLabel_1.font = .systemFont(ofSize: 15)
textLabel_1.textAlignment = .center
view.addSubview(textLabel_1)
}
//實現(xiàn)傳值按鈕的監(jiān)聽方法
@objc func tiaozhuanyemianclick(sender:UIButton){
//使用UserDefaults把需要傳的值幔托,先存到本地沙盒中,存儲是根據(jù)key來存儲的蜂挪,一個key對應(yīng)一個值(可以是可變數(shù)組重挑,或可變字典)
/*
Swift之UserDefaults數(shù)據(jù)存儲 http://www.reibang.com/p/99660cadfe16
1、存儲各個類型的數(shù)據(jù)
2棠涮、獲取各個類型的存儲數(shù)據(jù)
*/
//聲明數(shù)組變量名谬哀,并填入數(shù)據(jù)
let array = ["寶馬", "奔馳", "奧迪", "蘭博基尼", "凱迪拉克", "法拉利"]
//通過key來存儲數(shù)組
UserDefaults().set(array, forKey: "userNameArray")
//聲明字典變量名,并填入數(shù)據(jù)
let dic = ["BM":"寶馬", "BC":"奔馳", "AD":"奧迪", "LBJN":"蘭博基尼", "KDLK":"凱迪拉克", "FLL":"法拉利"]
//通過key來存儲字典
UserDefaults().set(dic, forKey: "userNameDictinary")
//通過key來存儲字符串
UserDefaults().set(textF.text!, forKey: "userName")
//返回上級頁面
self.navigationController?.popViewController(animated: true)
}
}
----------------------------------分割線---------------------------------------
傳值的幾種方式故爵,根據(jù)自己的需求來使用玻粪,其中隅津,有幾個可以實現(xiàn)正向傳值、反向傳值劲室,這里就不再寫了伦仍,太長了,也太累很洋,原諒我的懶惰吧充蓝!
代碼請到gitee上下載:https://gitee.com/h8900961/pass-value-demo
有任何問題,請留言喉磁,我會第一時間去處理谓苟。
謝謝,瀏覽协怒!