IOS學習筆記(頁面傳值:屬性傳值,協議代理傳值,閉包傳值)

Myboke110發表於2016-11-04

1.屬性傳值,代理協議傳值

建立檢視控制區,導航檢視控制器

在AppDelegate類下

 func application()方法下

  let vc = ViewController()

        let navc = UINavigationController(rootViewController: vc)

        

        window = UIWindow(frame: UIScreen.mainScreen().bounds)

        window?.rootViewController = navc

        window?.makeKeyAndVisible()


ViewController類下

var labelStr:UILabel?//設定全域性變數labelStr


class ViewController: UIViewController {

    

    override func viewDidLoad() {

        super.viewDidLoad()

     

        view.backgroundColor = UIColor.whiteColor()  //設定背景顏色

        navigationItem.title = "first"//設定導航欄標題

    

        

        labelStr = UILabel(frame: CGRectMake(100, 300, 100, 30))//設定labelStr的座標,寬高

        labelStr!.text = "海賊王"

        labelStr!.textColor = UIColor.blackColor()

        view.addSubview(labelStr!)

        

        

        let nextBtn = UIButton(frame: CGRectMake(100, 400, 80, 30)) //設定按鈕

        nextBtn.setTitleColor(UIColor.redColor(), forState: .Normal)

        nextBtn.setTitle("下一頁", forState: UIControlState.Normal)


        nextBtn.backgroundColor = UIColor.yellowColor()

        nextBtn.addTarget(self, action: "leftBtn", forControlEvents: UIControlEvents.TouchUpInside)//設定nextBtn的點選事件

        view.addSubview(nextBtn)

        

    }


    func leftBtn(){  

        let vc2 = ViewController2()

//屬性傳值        

        vc2.labelStr = labelStr!.text

        //將當前控制器設定vc2的代理

        vc2.delegate = self

        navigationController?.pushViewController(vc2, animated: true)//跳轉到ViewController2      

    }

//完成協議內容

extension ViewController:secondProtocol{

    func translateStr(str: String) {

//將ViewController的label.text 設定為 ViewController2 中的str  

        labelStr?.text = str

    }

}



在ViewController2類下

import UIKit

//從後往前傳值的協議

protocol secondProtocol {

    //需要傳什麼型別的引數,引數列表就寫什麼

    func translateStr(str:String)

}



class ViewController2: UIViewController {

    

    //定義代理屬性

    var delegate:secondProtocol?

    //定義一個labelStr屬性負責接收來自前一個頁面傳過來的值

    var labelStr:String?

    var textFeild:UITextField?

    


    override func viewDidLoad() {

        super.viewDidLoad()


        navigationItem.title = "Second"

      

        view.backgroundColor = UIColor.grayColor()

        

//設定文字框

        textFeild! = UITextField(frame: CGRectMake(100, 300, 180, 30))

        textFeild!.placeholder = "請輸入文字"

        textFeild?.text = labelStr

        view.addSubview(textFeild!)

       

        

        //設定按鈕

        let nextBtn = UIButton(frame: CGRectMake(100, 400, 80, 30))

        nextBtn.setTitle("上一頁", forState: UIControlState.Normal)

        nextBtn.setTitleColor(UIColor.redColor(), forState: .Normal)

        nextBtn.backgroundColor = UIColor.yellowColor()

        nextBtn.addTarget(self, action: "rightBtn", forControlEvents: UIControlEvents.TouchUpInside)

        

        view.addSubview(nextBtn)

    }

    


    

    func rightBtn(){

//執行協議

        delegate?.translateStr((textFeild?.text!)!)

        navigationController?.popViewControllerAnimated(true)

    }

    

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

       

    }

    

}



2.閉包傳值,用閉包傳值改變上一頁的背景顏色

ViewController類下

import UIKit


class ViewController: UIViewController {


    override func viewDidLoad() {

        super.viewDidLoad()

        

        navigationItem.title = "first"

        view.backgroundColor = UIColor.whiteColor()

        let btn = UIButton(frame: CGRectMake(100, 100, 100, 50))

        btn.layer.borderWidth = 2

        btn.setTitle("Push", forState: .Normal)

        btn.setTitleColor(UIColor.blackColor(), forState: .Normal)

        btn.addTarget(self, action: "btn", forControlEvents: UIControlEvents.TouchUpInside)

        view.addSubview(btn)

        

    }


    func btn(){

        let vc2 = ViewController2()

//閉包的實現

        vc2.color = {

            (str:UIColor) -> Void

            in

            self.view.backgroundColor = str

        

        }

        

        

        navigationController?.pushViewController(vc2, animated: true)

    }

    


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }



}


ViewController2類下

import UIKit

class ViewController2: UIViewController {


    var color:((str:UIColor)->Void)?//設定閉包

        override func viewDidLoad() {

        super.viewDidLoad()


        navigationItem.title = "Second"

        view.backgroundColor = UIColor.whiteColor()

        let btn = UIButton(frame: CGRectMake(100, 100, 100, 50))

        btn.setTitle("Pop", forState: .Normal)

        btn.setTitleColor(UIColor.blackColor(), forState: .Normal)

        btn.layer.borderWidth = 2

        btn.addTarget(self, action: "btn", forControlEvents: UIControlEvents.TouchUpInside)

        

        view.addSubview(btn)

        

        

        

    }

    

    func btn(){


        let str = UIColor.blueColor()//設定str為藍色

//呼叫閉包

        color!(str: str)

        navigationController?.popViewControllerAnimated(true)

            }

    


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

    

    }

    }



相關文章