《Swift 的界面之间的传值,详细介绍3种传值方式》 正向传值、协议传值、闭包

前端之家收集整理的这篇文章主要介绍了《Swift 的界面之间的传值,详细介绍3种传值方式》 正向传值、协议传值、闭包前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Swift 的界面之间的传值,详细介绍3种传值方式》正向传值、协议传值、闭包

1、 控制器界面间的正向传值

/*

Swift 的界面之间的传值,详细介绍3种传值方式》

正向传值、协议传值、闭包

*/

随着移动的开发,界面之间的传值,也频繁出现,更关键的是在面试的时候也时常被提起。提起最多的是 Block 同时还有协议 (在Swift中是叫 闭包)。我们要详细介绍它。

// Created by 周双建 on 15/12/7.

// Copyright © 2015 周双建. All rights reserved.

//


import UIKit


class ViewController: UIViewController {

let Screen = CGSizeMake(

200,40)

override func viewDidLoad() {

super.viewDidLoad()

// 我们首先,进行正面解说

//我们先给我们的本控制器设置一个标题 (必须实现导航控制器否者不显示)

self.title = "第一个控制器"

self.view.backgroundColor = UIColor.whiteColor()

// 设置一个点击按钮,驱动事件跳转到下一个控制器

let Btn = UIButton(type: UIButtonType.Custom) as UIButton

Btn.frame = CGRectMake(20, 100,Screen.width,Screen.height)

Btn.setTitle("跳转到第二个控制器",forState: UIControlState.Normal)

Btn.setTitleColor(UIColor.magentaColor(),forState: UIControlState.Normal)

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

self.view.addSubview(Btn)

// Do any additional setup after loading the view,typically from a nib.

}

/*****************************************************************/

// 按钮点击后,事件的触发

func BtnClick(){

//获取跳转的控制器对象

let TVC = TwoViewController()

//传入一个字符串,给第二个控制器

TVC.TSTR = "第二个控制器"

//在传入一个颜色对象

TVC.TColoer = UIColor.blueColor()

//实现界面的跳转

self.navigationController?.pushViewController(TVC,animated: true)

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}


第二个控制器的内容是:

//

// TwoViewController.swift

// Swift_009

//


import UIKit


class TwoViewController: UIViewController {

//首先,我们要创建两个对象,来接受上一个界面传来的值

var TSTR :String? // 创建一个字符串对象

var TColoer :UIColor? //创建一个UIColoer 的颜色对象

override func viewDidLoad() {

super.viewDidLoad()

//设置控制器的标题

self.title = self.TSTR

// 设置背景颜色

self.view.backgroundColor = self.TColoer

// Do any additional setup after loading the view.

}


override func didReceiveMemoryWarning() {

// Dispose of any resources that can be recreated.

}

}


2、 协议的反向传值

// FViewController.swift

//


import UIKit

/*

注意了:如果不实现,你的定义的协议方法,会一直报错 。如下:Type 'FViewController' does not conform to protocol 'FTvCdelegte'

解决方法就是:实现你定义的协议方法

*/

class FViewController: UIViewController,FTVCdelegte{


override func viewDidLoad() {

super.viewDidLoad()

//我们要创建一个返回按钮

let Fbtn = UIButton(type: UIButtonType.Custom) as UIButton

Fbtn.frame = CGRectMake(30,self.view.frame.size.width-60,40)

Fbtn.setTitle("跳转到第二个界面",forState: UIControlState.Normal)

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

Fbtn.setTitleColor(UIColor.redColor(),forState: UIControlState.Normal)

self.view.addSubview(Fbtn)


// Do any additional setup after loading the view.

}

/****************************************************************/

//跳转触发事件

func Click(){

let Tvc = FtwoViewController()

Tvc.delegate_zsj = self

self.navigationController?.pushViewController(Tvc,animated: true)

}

override func didReceiveMemoryWarning() {

// Dispose of any resources that can be recreated.

}

//实现协议的方法

//更改主题名字

func change(title: String) {

self.title = title

}

//更改背景色

func ChangeColoer(Coloer: UIColor) {

self.view.backgroundColor = Coloer

}

//是否成功

func ChangSucces(YON: Bool) {

print(YON)

}

/*

// MARK: - Navigation


// In a storyboard-based application,you will often want to do a little preparation before navigation

override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) {

// Get the new view controller using segue.destinationViewController.

// Pass the selected object to the new view controller.

}

*/


}


第二个控制器的内容是:

// FtwoViewController.swift

//


import UIKit

//要创建一个协议

protocol FTVCdelegte : NSObjectProtocol{

//在协议里面,声明许多方法

// 第一个,改变标题

func change(title:String)

//第二个,改变背景色

func ChangeColoer (Coloer:UIColor)

//是否成功的标志

func ChangSucces(YON:Bool)

}

class FtwoViewController: UIViewController {

// 创建一个准守协议的对象

var delegate_zsj :FTVCdelegte?

override func viewDidLoad() {

super.viewDidLoad()

返回上一个界面",forState: UIControlState.Normal)

self.view.addSubview(Fbtn)

/************************************************************/

//返回,事件的触发

func Click(){

delegate_zsj?.change("首页")

delegate_zsj?.ChangeColoer(UIColor.redColor())

delegate_zsj?.ChangSucces(true)

self.navigationController?.popToRootViewControllerAnimated(true)

}

override func didReceiveMemoryWarning() {

// Dispose of any resources that can be recreated.

}


/*

*/


}


3、闭包(Block)反向传值

// BBViewController.swift

//


import UIKit


class BBViewController: UIViewController {


override func viewDidLoad() {

super.viewDidLoad()

//创建一个按钮

let btn = UIButton(type: UIButtonType.Custom) as UIButton

btn.frame = CGRectMake(30,40)

btn.setTitle("UIControlState.Normal)

btn.setTitleColor(UIColor.magentaColor(),forState: UIControlState.Normal)

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

self.view.addSubview(btn)

// Do any additional setup after loading the view.

}

func Click(){

let BBTC = BBTViewController()

BBTC.bbchange = { (title:String,coloer:UIColor) in

self.title = title

self.view.backgroundColor = coloer

}

self.navigationController?.pushViewController(BBTC,0)"> */


}


第二个控制器是:

// BBTViewController.swift

//


import UIKit


class BBTViewController: UIViewController {

//定义一个闭包,带有两个参数

var bbchange :((title:String,coloer:UIColor)->Void)?

override func viewDidLoad() {

super.viewDidLoad()

self.view.backgroundColor = UIColor.greenColor()

//创建一个按钮

返回到第1个控制器",action: "bClick",forControlEvents: UIControlEvents.TouchUpInside)

self.view.addSubview(btn)


// Do any additional setup after loading the view.

}

func bClick(){

bbchange?(title:"成龙",coloer:UIColor.redColor())

self.navigationController?.popToRootViewControllerAnimated(true)

}

override func didReceiveMemoryWarning() {

*/


}

猜你在找的Swift相关文章