Swift的四種傳值方法

Swift中常用的四種傳值方法:單例,單例,閉包(相當于OC的block傳值),通知

1單例:

1>.創(chuàng)建單例變量 在AppDelegate.Swift中創(chuàng)建 var backgroundColor:UIColor?

2>.在頁面A中創(chuàng)建單例的對象:

//? ? ? ? 創(chuàng)建單例對象

func changeBlue() {

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

appDelegate.backgroundColor = UIColor.blueColor();

self.dismissViewControllerAnimated(true, completion: nil)

}

3>.在頁面B中重寫弗雷方法,并調用單例的變量完成數值的傳遞:

//重寫父類方法,并且調用單例的變量完成數值的傳遞

override func viewWillAppear(animated: Bool) {

super.viewWillAppear(animated)

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

self.view.backgroundColor = appDelegate.backgroundColor

2.代理

1>.頁面A

//定義一個協(xié)議和方法

protocol GetMessageDelegate:NSObjectProtocol

{

//? ? 回調方法? 傳入一個String類型的值

func getMessage(controller:AgentSecondVC,string:String)

}

聲明delegate屬性

var delegate:GetMessageDelegate?

設置代理方法:

func goBack() {

//? ? ? ? 調用代理方法

if ((delegate) != nil) {

delegate?.getMessage(self, string:_textField!.text!)

self.navigationController?.popViewControllerAnimated(true)

}

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

B頁面:

//? ? 接收傳過來的值

func getMessage(controller: AgentSecondVC, string: String) {

_label.text = string

if string == ""

{

_label.text = "null"

}

}

}

3.閉包

1>.在A頁面的視圖控制器中聲明一個閉包:

//定義閉包類型(特定的函數類型函數類型)

typealias InputClosureType = (String) ->Void

2>.

func clickBtn() {

//? ? ? ? 創(chuàng)建閉包方法

if? self.backClosure != nil

{

if let tempString = self.textField.text

{

self.backClosure!(tempString)

}

}

self.navigationController?.popViewControllerAnimated(true)

}

3>.在頁面B中實現閉包的回調

func click()? {

let closureSecondVC = ClosureSecondVC()

//? ? ? ? 實現回調,獲取回調的數據(閉包)

closureSecondVC.backClosure = {

(backStr:String)->Void in

self.showLabel.text = backStr

}

self.navigationController?.pushViewController(closureSecondVC, animated: true)

}

4.通知

1>.發(fā)送通知

let dict = ["name":"hello"]

NSNotificationCenter .defaultCenter().postNotificationName("NotificationIdentifier", object: dict, userInfo: dict)

self.navigationController?.popViewControllerAnimated(true)

2>.接受通知

//? ? ? ? 接受通知

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(NotificationFristVC.getMyName(_:)), name: "NotificationIdentifier", object: nil)

3>.刪除通知

//? ? ? ? 刪除通知

NSNotificationCenter.defaultCenter().removeObserver(self)

//? ? 和上面的刪除通知效果一樣的

NSNotificationCenter.defaultCenter().removeObserver(self, name: "NotificationIdentifier", object: nil)

4>.獲取通知的方法

func getMyName(notification:NSNotification)

{

//? ? ? ? 獲取詞典中的值

let name = notification.object?.valueForKey("name") as? String

//? ? ? ? 通知名稱

let nameNotification = notification.name

//? ? ? notification.userInfo 接收object 對象 一些信息 例如入鍵盤的一些信息

print(nameNotification)

print(name)

}

關于Swift的四種傳值方法的具體代碼請參考:https://github.com/wangningsai/Swift_ByVal/tree/master

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容