1. 程式人生 > >Swift 傳值之通知傳值

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)
}

}

相關推薦

Swift 通知

通知傳值記住使用完通知時移除通知,工程建立和屬性,代理,閉包傳值一樣 屬性傳值:http://blog.csdn.net/zhangjitao_boke/article/details/5060618

元件子、子

父元件傳值給子元件 父元件 <template> <div id="app"> <h1>props使用方式</h1> <hello txt='元件txt' v-bind:ddd="btn

Vue元件通訊父子---子

<!DOCTYPE html> <html lang="en"> <head> <title></title> <meta charset="UTF-8"> <meta name="vi

html url

今天做h5的時候  由於loaclStorage 中不想寫太多的東西 然後只能想辦法傳值百度了一下URL傳值,摸索然後記錄分享一下 var data1 = JSON.stringify(data);

java 通過後端訪問http並post

String param= "{\n" + "\"signature\":\"1b811b104815e80dc06204769abf019454918820\",\n" + "\"timestamp\":\"63613249133781753

Swift通知

通知方 NotificationCenter.default.post(name: NSNotification.Name(rawValue:"s"), object:nil, userInfo:["one":languageField!.text! , "two":EnglishF

Swift 閉包

Swift的閉包傳值和OC中的block傳值非常相似 專案的建立和之前的屬性傳值和代理傳值類似  屬性傳值:http://blog.csdn.net/zhangjitao_boke/article/

【swift_4】swift代理(delegate的用法)

2017.03.16更新簡潔版 protocol ChildDelegate:class{ func childDidSomething()} classChild{ weak va

swift詳解二十二-----------UINavigationController的基本用法和頁面幾種方式

UINavigationController的基本用法和頁面傳值幾種方式 本文介紹UINavigationController基本用法,因為涉及多頁面順便介紹頁面傳值 1、手寫程式碼建立UINavigationController 手寫方式建立很簡

iOS Block、代理通知中心

  Joker_King  關注 2016.04.18 22:17*  字數 802  閱讀 1792 評論 7 喜歡 12 在我們需

vue非父子元件釋出訂閱模式

vue非父子元件傳值之釋出訂閱模式 目前瞭解的元件傳值的幾種方式 props 再熟悉不過了 vuex 也比較常用 evenbus 釋出訂閱 主要說一下發布訂閱 Vue.prototype.bus = new Vue();

vue元件

vue元件間的資料傳遞分為兩種:傳值和傳引用 1.傳引用 將User中的資料拿到app中作為共用資料 使用<user v-bind:users="users"></user>方法向子元件傳值,子元件通過 props:{users:{type:Array,

Vue.js父子

官網連結:https://cn.vuejs.org/v2/guide/components-props.html 視訊連結: https://ke.qq.com/webcourse/index.html#cid=329091&term_id=100390520&taid=25

Java學習筆記物件和引用總結

<strong><span style="font-size:18px;"> public class Test { public static void main(String[] args) { // TODO Auto-generated method s

react傳遞資料的幾種方式props、路由、狀態提升、redux、context

react之傳遞資料的幾種方式 1、父子傳值 父傳值:<子的標籤 value={'aaa'} index={'bbb'}></子的標籤> 子接值:<li key={this.props.index}>{this.props.value}</li>

curl進行 (post)

<?php header("content-type:text/html;charset=utf-8"); function curlPost($url,$data='',$method){ $ch = curl_init(); //1.初始化 curl_setopt($c

通知 ( 多傳送 , 1接收)

三個通知方: 第一個 NSDictionary *dic = @{@"title":TextField.text,@"typeID":@(1)}; [[NSNotificationCenter defaultCenter] postNotificatio

通知

通知傳值 ( 多傳送 , 1接收) 三個通知方: 第一個 NSDictionary *dic = @{@"title":TextField.text,@"typeID":@(1)}; [[NSNotificationCenter defaultCenter] postNotific

iframePostmessage

緣由: 最近遇到一個神奇的問題,淘寶WindVane不支援多層巢狀,只支援一層巢狀的形式,所以就像使用一層傳值的方式來互動iframe,調研了幾種方案以後,發現還是window.postMessage傳值在iframe最靠譜,但是相對的要改動原來的頁面,所以自己衡量啦~

iOS OC與H5網頁互動OC給JS(WKWebView)

1.OC傳值給JS的程式碼:// 頁面載入完成之後呼叫 - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)