您的位置:首页 > 移动开发 > IOS开发

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

2016-11-07 14:23 363 查看
//  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()
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios swift ios开发 通信
相关文章推荐