Swift中界面傳值的方法 主要有三種
1.代理傳值
2.閉包傳值(即OC中的Block)
- 屬性傳值
代理傳值
First頁面
class FirstViewController: UIViewController ,ValueDelegate {
//設置屬性label 后面加個! 代表在需要的時候再初始化
var label : UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//設置導航視圖控制器的右邊按鈕
//在Swift中設置枚舉值時候使用的是枚舉類名 + . 枚舉名 在這里系統幫我們自動省略掉類名
//在設置事件的時候 ("方法名")
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "jumpToSecondVCClick")
//初始化label的位置
label = UILabel(frame: CGRect(x: 100, y: 100, width: 200, height: 50))
//設置label的背景顏色
label.backgroundColor = UIColor.cyanColor()
//將label添加到視圖上
self.view.addSubview(label)
}
//聲明導航視圖控制器的按鈕 點擊事件
func jumpToSecondVCClick() {
let secondVC = SecondViewController()
//設置代理
secondVC.delegate = self
//跳轉到第二個頁面
self.showViewController(secondVC, sender: nil)
}
//實現代理方法
func valueClicked(string: String) {
label.text = string
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Second頁面
//聲明協議 同OC相同 還是需要寫到類的上面
protocol ValueDelegate {
//聲明代理方法
func valueClicked(string : String)
}
class SecondViewController: UIViewController {
//設置代理屬性 必須置為nil
var delegate : ValueDelegate? = nil
//設置輸入框屬性
var TF : UITextField!
override func viewDidLoad() {
super.viewDidLoad()
//設置頁面的顏色
self.view.backgroundColor = UIColor.whiteColor()
//設置導航視圖控制器的左邊按鈕
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Cancel, target: self, action: "jumpToFristVCClick")
//初始化輸入框并設置frame
TF = UITextField(frame: CGRect(x: 100, y: 100, width: 200, height: 50))
TF.backgroundColor = UIColor.cyanColor()
//將輸入框添加到視圖上
self.view.addSubview(TF)
}
//設置跳轉時間
func jumpToFristVCClick() {
//當跳轉到Frist頁面的時候 設置代理將輸入款輸入的文字傳到First頁面
self.delegate?.valueClicked(TF.text!)
//跳轉到First頁面
self.navigationController?.popViewControllerAnimated(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
閉包傳值
關于UI的代碼與上面的一模一樣 只是傳值的方式不一樣,在下面就不添加過多的注釋了
First頁面
class FirstViewController: UIViewController{
var label : UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "jumpToSecondVCClick")
label = UILabel(frame: CGRect(x: 100, y: 100, width: 200, height: 50))
label.backgroundColor = UIColor.cyanColor()
self.view.addSubview(label)
}
func jumpToSecondVCClick() {
let secondVC = SecondViewController()
//將傳過來的值 賦給label
secondVC.sendValueClsure = { (string : String) -> Void in
self.label.text = string
}
self.showViewController(secondVC, sender: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Second頁面
//重命名一個閉包
typealias sendValue = (string : String) -> Void
class SecondViewController: UIViewController {
//創建一個閉包屬性
var sendValueClsure : sendValue?
var TF : UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor()
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Cancel, target: self, action: "jumpToFristVCClick")
TF = UITextField(frame: CGRect(x: 100, y: 100, width: 200, height: 50))
TF.backgroundColor = UIColor.cyanColor()
self.view.addSubview(TF)
}
func jumpToFristVCClick() {
//將值附在閉包上,傳到First頁面
self.sendValueClsure!(string: TF.text!)
self.navigationController?.popViewControllerAnimated(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
屬性傳值
UI與上兩個基本一樣,將First頁面的UILabel換成UITextField 即可
Frist頁面
class FirstViewController: UIViewController{
var TF : UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "jumpToSecondVCClick")
TF = UITextField(frame: CGRect(x: 100, y: 100, width: 200, height: 50))
TF.backgroundColor = UIColor.cyanColor()
self.view.addSubview(TF)
}
func jumpToSecondVCClick() {
let secondVC = SecondViewController()
//將輸入框中輸入的文字賦值給Second控制器的屬性string
secondVC.string = TF.text
self.showViewController(secondVC, sender: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Second頁面
class SecondViewController: UIViewController {
var string : String? = nil
var TF : UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor()
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Cancel, target: self, action: "jumpToFristVCClick")
TF = UITextField(frame: CGRect(x: 100, y: 100, width: 200, height: 50))
TF.backgroundColor = UIColor.cyanColor()
//將從First頁面傳回來的string的值賦給Second的TF
TF.text = string
self.view.addSubview(TF)
}
func jumpToFristVCClick() {
self.navigationController?.popViewControllerAnimated(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
在這三天的Swift學習中 ,我發現 在使用時 ,其實大部分用到了OC中的方法和思想,只是Swift相比OC的代碼格式上更加簡潔和易記
由此可見,如果有OC基礎的學習者們,在學習Swift的時候,只需要研究下Swift和OC的格式區別對于上手Swift還是相當快的