Swift界面?zhèn)髦?/h2>

Swift中界面?zhèn)髦档姆椒? 主要有三種

1.代理傳值
2.閉包傳值(即OC中的Block)

  1. 屬性傳值

代理傳值

First頁(yè)面

class FirstViewController: UIViewController ,ValueDelegate {

    //設(shè)置屬性label   后面加個(gè)!  代表在需要的時(shí)候再初始化
    var label : UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
 //設(shè)置導(dǎo)航視圖控制器的右邊按鈕
  //在Swift中設(shè)置枚舉值時(shí)候使用的是枚舉類(lèi)名 + . 枚舉名    在這里系統(tǒng)幫我們自動(dòng)省略掉類(lèi)名  
//在設(shè)置事件的時(shí)候 ("方法名")
 self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "jumpToSecondVCClick")
  
        //初始化label的位置
        label = UILabel(frame: CGRect(x: 100, y: 100, width: 200, height: 50))
        //設(shè)置label的背景顏色
        label.backgroundColor = UIColor.cyanColor()
        //將label添加到視圖上
        self.view.addSubview(label)
    }
    
    //聲明導(dǎo)航視圖控制器的按鈕  點(diǎn)擊事件
    func jumpToSecondVCClick() {
        let secondVC = SecondViewController()
        
        //設(shè)置代理
        secondVC.delegate = self
        //跳轉(zhuǎn)到第二個(gè)頁(yè)面
        self.showViewController(secondVC, sender: nil)
    }
    

    //實(shí)現(xiàn)代理方法
    func valueClicked(string: String) {
        label.text = string
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
     
    }
    
}

Second頁(yè)面


//聲明協(xié)議   同OC相同 還是需要寫(xiě)到類(lèi)的上面

protocol ValueDelegate {
    //聲明代理方法
    func valueClicked(string : String)
}

class SecondViewController: UIViewController  {

    //設(shè)置代理屬性  必須置為nil
    var delegate : ValueDelegate? = nil
   
    //設(shè)置輸入框?qū)傩?    var TF : UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        //設(shè)置頁(yè)面的顏色
        self.view.backgroundColor = UIColor.whiteColor()

//設(shè)置導(dǎo)航視圖控制器的左邊按鈕        
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Cancel, target: self, action: "jumpToFristVCClick")

       //初始化輸入框并設(shè)置frame
       TF = UITextField(frame: CGRect(x: 100, y: 100, width: 200, height: 50))
        TF.backgroundColor = UIColor.cyanColor()
        //將輸入框添加到視圖上
        self.view.addSubview(TF)
    }
    
   //設(shè)置跳轉(zhuǎn)時(shí)間
    func jumpToFristVCClick() {

//當(dāng)跳轉(zhuǎn)到Frist頁(yè)面的時(shí)候  設(shè)置代理將輸入款輸入的文字傳到First頁(yè)面        
self.delegate?.valueClicked(TF.text!)

//跳轉(zhuǎn)到First頁(yè)面        
self.navigationController?.popViewControllerAnimated(true)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}


閉包傳值

關(guān)于UI的代碼與上面的一模一樣 只是傳值的方式不一樣,在下面就不添加過(guò)多的注釋了

First頁(yè)面

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()
        //將傳過(guò)來(lái)的值  賦給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頁(yè)面

//重命名一個(gè)閉包
typealias sendValue = (string : String) -> Void

class SecondViewController: UIViewController  {

    //創(chuàng)建一個(gè)閉包屬性
    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頁(yè)面
        self.sendValueClsure!(string: TF.text!)
        self.navigationController?.popViewControllerAnimated(true)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

屬性傳值
UI與上兩個(gè)基本一樣,將First頁(yè)面的UILabel換成UITextField 即可

Frist頁(yè)面



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頁(yè)面

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頁(yè)面?zhèn)骰貋?lái)的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學(xué)習(xí)中 ,我發(fā)現(xiàn) 在使用時(shí) ,其實(shí)大部分用到了OC中的方法和思想,只是Swift相比OC的代碼格式上更加簡(jiǎn)潔和易記
由此可見(jiàn),如果有OC基礎(chǔ)的學(xué)習(xí)者們,在學(xué)習(xí)Swift的時(shí)候,只需要研究下Swift和OC的格式區(qū)別對(duì)于上手Swift還是相當(dāng)快的

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

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容