Swift 传值之通知传值

通知传值记住使用完通知时移除通知,工程建立和属性,代理,闭包传值一样

属性传值:http://blog.csdn.net/zhangjitao_boke/article/details/50606185

代理传值:http://blog.csdn.net/zhangjitao_boke/article/details/50606707

闭包传值:http://blog.csdn.net/zhangjitao_boke/article/details/50607325

单例传值:http://blog.csdn.net/zhangjitao_boke/article/details/50608066


代码 

FirstViewController


//
// FirstViewController.swift
// NotificationPassValue
//
// Created by JT on 16/1/29.
// Copyright © 2016年 JT. All rights reserved.
//

import UIKit

class FirstViewController: UIViewController {

@IBOutlet weak var firstTextField: UITextField!

override func viewDidLoad() {
super.viewDidLoad()
}

// 收到通知post后执行方法
func getValue(notification:NSNotification) {
// 赋值
firstTextField.text = notification.object as? String
// 移除通知
NSNotificationCenter.defaultCenter().removeObserver(self, name: "passValue", object: nil)
}


override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// 添加通知
let center = NSNotificationCenter.defaultCenter()
center.addObserver(self, selector: "getValue:", name: "passValue", object: nil)


let storyBoard = UIStoryboard.init(name: "Main", bundle: nil)
let secondVC = storyBoard.instantiateViewControllerWithIdentifier("secondVC")
self.navigationController?.pushViewController(secondVC, animated: true)
}


}

SecondViewController 代码:

//
// SecondViewController.swift
// NotificationPassValue
//
// Created by JT on 16/1/29.
// Copyright © 2016年 JT. All rights reserved.
//

import UIKit

class SecondViewController: UIViewController {

@IBOutlet weak var secondTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()

self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: UIBarButtonItemStyle.Plain, target: self, action: "backAction")

}
func backAction() {
// 发送通知
let center = NSNotificationCenter.defaultCenter()
center.postNotificationName("passValue", object: secondTextField.text!)

self.navigationController?.popViewControllerAnimated(true)
}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值