IOS开发基础06(界面通信(界面传值):属性传值、代理传值、闭包传值)

//  AppDelegate.swift


import UIKit


@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {


    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        //设置导航视图控制器

        let vc = ViewController()

        let navc = UINavigationController(rootViewController: vc)

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

        window?.backgroundColor = UIColor.whiteColor()

        window?.rootViewController = navc

        window?.makeKeyAndVisible()

        

        return true

    }


    func applicationWillResignActive(application: UIApplication) {

    }


    func applicationDidEnterBackground(application: UIApplication) {

    }


    func applicationWillEnterForeground(application: UIApplication) {

    }


    func applicationDidBecomeActive(application: UIApplication) {

    }


    func applicationWillTerminate(application: UIApplication) {

    }

}


// ViewController.swift


import UIKit


/*

    界面通信:界面传值

    1.从前往后传

        ——属性传值

    2.从后往前传

        ——1.代理传值

        ——2.closure(闭包)传值

*/


class ViewController: UIViewController {


    var label0:UILabel?


    override func viewDidLoad() {

        super.viewDidLoad()

        

        navigationItem.title = "VC01"

        

        label0 = UILabel(frame: CGRectMake(140, 150, 100, 40))

        label0!.text = "死神"

        label0!.textColor = UIColor.blueColor()

        label0!.textAlignment = .Center

        view.addSubview(label0!)

        

        let nextBtn = UIBarButtonItem(title: "next", style: UIBarButtonItemStyle.Plain, target: self, action: "nextAction")

        navigationItem.rightBarButtonItem = nextBtn

        

    }

    

    func nextAction(){

        let secondVC = SecondViewController()

        

        //sencondVC定义具体的闭包方法(从后往前————闭包传值)

        secondVC.color = {

            (str:UIColor) -> Void

            in

            self.view.backgroundColor = str

        }

        

        //将当前的控制器设置为secondVC的代理(从后往前————代理传值)

        secondVC.delegate = self

        

        //传值(从前往后————属性传值)

        secondVC.labelStr = label0?.text

        navigationController?.pushViewController(secondVC, animated: true)

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

    }

}

//设置了代理就一定要遵循协议

extension ViewController:secondPro {

    func translateString(str: String) {

        label0?.text = str

    }

}


//  SecondViewController.swift


import UIKit


//从后往前传值的协议

protocol secondPro {

    //需要传什么类型的参数,参数列表就写什么

    func translateString(str:String)

}

class SecondViewController: UIViewController {

    

    //定义一个闭包

    var color:((str:UIColor)->Void)?

    

    //定义labelStr属性负责接受来自前一个页面传过来的值

    var labelStr:String?

    var textField:UITextField?

    

    //定义代理属性

    var delegate:secondPro?


    override func viewDidLoad() {

        super.viewDidLoad()


        view.backgroundColor = UIColor.whiteColor()

        navigationItem.title = "VC02"

        

        let backBtn = UIBarButtonItem(title: "<back", style: UIBarButtonItemStyle.Plain, target: self, action: "backAction")

        navigationItem.leftBarButtonItem = backBtn

                

        textField = UITextField(frame: CGRectMake(100, 100, 200, 40))

        textField!.placeholder = "请输入文字"

        textField!.borderStyle = .RoundedRect

        textField?.text = labelStr

        view.addSubview(textField!)        

    }

    

    func backAction(){

        //调用闭包方法

        let str = UIColor.blueColor()

        color!(str:str)

        

//调用协议

        delegate?.translateString((textField?.text)!)


        navigationController?.popViewControllerAnimated(true)

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

    }

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值