Swift的四种传值方法

Swift中常用的四种传值方法:单例,单例,闭包(相当于OC的block传值),通知

1单例:

1>.创建单例变量 在AppDelegate.Swift中创建 var backgroundColor:UIColor?

2>.在页面A中创建单例的对象:

//        创建单例对象

func changeBlue() {

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

appDelegate.backgroundColor = UIColor.blueColor();

self.dismissViewControllerAnimated(true, completion: nil)

}

3>.在页面B中重写弗雷方法,并调用单例的变量完成数值的传递:

//重写父类方法,并且调用单例的变量完成数值的传递

override func viewWillAppear(animated: Bool) {

super.viewWillAppear(animated)

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

self.view.backgroundColor = appDelegate.backgroundColor

2.代理

1>.页面A

//定义一个协议和方法

protocol GetMessageDelegate:NSObjectProtocol

{

//    回调方法  传入一个String类型的值

func getMessage(controller:AgentSecondVC,string:String)

}

声明delegate属性

var delegate:GetMessageDelegate?

设置代理方法:

func goBack() {

//        调用代理方法

if ((delegate) != nil) {

delegate?.getMessage(self, string:_textField!.text!)

self.navigationController?.popViewControllerAnimated(true)

}

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

B页面:

//    接收传过来的值

func getMessage(controller: AgentSecondVC, string: String) {

_label.text = string

if string == ""

{

_label.text = "null"

}

}

}

3.闭包

1>.在A页面的视图控制器中声明一个闭包:

//定义闭包类型(特定的函数类型函数类型)

typealias InputClosureType = (String) ->Void

2>.

func clickBtn() {

//        创建闭包方法

if  self.backClosure != nil

{

if let tempString = self.textField.text

{

self.backClosure!(tempString)

}

}

self.navigationController?.popViewControllerAnimated(true)

}

3>.在页面B中实现闭包的回调

func click()  {

let closureSecondVC = ClosureSecondVC()

//        实现回调,获取回调的数据(闭包)

closureSecondVC.backClosure = {

(backStr:String)->Void in

self.showLabel.text = backStr

}

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

}

4.通知

1>.发送通知

let dict = ["name":"hello"]

NSNotificationCenter .defaultCenter().postNotificationName("NotificationIdentifier", object: dict, userInfo: dict)

self.navigationController?.popViewControllerAnimated(true)

2>.接受通知

//        接受通知

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(NotificationFristVC.getMyName(_:)), name: "NotificationIdentifier", object: nil)

3>.删除通知

//        删除通知

NSNotificationCenter.defaultCenter().removeObserver(self)

//    和上面的删除通知效果一样的

NSNotificationCenter.defaultCenter().removeObserver(self, name: "NotificationIdentifier", object: nil)

4>.获取通知的方法

func getMyName(notification:NSNotification)

{

//        获取词典中的值

let name = notification.object?.valueForKey("name") as? String

//        通知名称

let nameNotification = notification.name

//      notification.userInfo 接收object 对象 一些信息 例如入键盘的一些信息

print(nameNotification)

print(name)

}

关于Swift的四种传值方法的具体代码请参考:https://github.com/wangningsai/Swift_ByVal/tree/master

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、通知传值 首先我们来看看通知传值,通知可实现任意界面之间的数据传递,但必须满足一个条件,就是保证在发送通知的时...
    Insit阅读 3,424评论 0 2
  • SwiftDay011.MySwiftimport UIKitprintln("Hello Swift!")var...
    smile丽语阅读 4,114评论 0 6
  • iOS开发中,页面传值是很常见的,但是页面传值你究竟知道多少呢?笔者这篇文章就是给大家介绍一下页面传值的具体方式,...
    蒲公英少年带我飞阅读 2,280评论 10 45
  • swift30快速入门 常量与变量 控制流 函数和闭包 对象和类 swift30-将元组加到数组中 swift-数...
    XLsn0w阅读 561评论 0 0
  • 1.自定义控件 a.继承某个控件 b.重写initWithFrame方法可以设置一些它的属性 c.在layouts...
    圍繞的城阅读 3,715评论 2 4

友情链接更多精彩内容