HOME > NSNotificationCenter
NSNotificationCenterでコード間のイベントやりとり
AppDelegateのイベント(アプリがアクティブ)になるタイミングでViewControllerに通知する為にはどうすればいいのか?と 調査しているとこのクラスにたどり着きました。delegateを利用せずにイベントの通知をViewController間で簡単に実装できるので 使いやすいです!
NSNotificationCenterのクラス階層
NSObject
↑
NSNotificationCenter
参考:Foundation Framework Reference NSNotificationCenter Class Reference
参考:SwiftでNSNotificationCenterを使う
参考:[iOS8] Swiftでアプリがバックグラウンド・フォアグラウンドになった時の処理(NSNotificationCenter)
参考:Swiftでいい感じにNSNotificationCenter#addObserverForNameを使う
参考:Swift で NSNotification の userInfo を扱う
やってみた
起動中のアプリがアクティブに切り替わった際に呼ばれるAppDelegateのイベントをサブビューのViewControllerで受け取る方法を記載してみます。
AppDelegate.swift(イベント登録側)
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
return true
}
//アプリがアクティブになった時によばれる関数
func applicationWillEnterForeground(application: UIApplication) {
NSNotificationCenter.defaultCenter().postNotificationName("applicationWillEnterForeground", object: nil)
}
...
}
ViewController.swft (イベント受け取り側)
import UIKit
import WebKit
class ViewController: UIViewController,WKNavigationDelegate,UITableViewDelegate, UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "viewWillEnterForeground:", name: "applicationWillEnterForeground", object: nil)
}
func viewWillEnterForeground(notification: NSNotification?){
println("呼ばれました!");
}
イベントリスナーを削除する場合
removeObeserverを利用して削除します。
NSNotificationCenter.defaultCenter().removeObserver(self)
カスタマイズ
① Notificatin通知にデータをくっつけて送る。ー> userInfoを利用する。
AppDelegate.swift(イベント登録側)
postNotificationNameにuserInfoパラメータを追加します。
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
return true
}
//postNotificationNameでuserInfoを追加
func applicationWillEnterForeground(application: UIApplication) {
NSNotificationCenter.defaultCenter().postNotificationName("applicationWillEnterForeground", object: nil, userInfo: ["testNumber": 123]);)
}
...
}
ViewController.swft (イベント受け取り側)
受け取り側でアンラップして取得します。
import UIKit
import WebKit
class ViewController: UIViewController,WKNavigationDelegate,UITableViewDelegate, UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "viewWillEnterForeground:", name: "applicationWillEnterForeground", object: nil)
}
func viewWillEnterForeground(notification: NSNotification?){
if let userInfo = notification?.userInfo {
let value = userInfo["testNumber"]! as Int
println(value) // 123
}
println("呼ばれました!");
}
まとめ
なによりもDelegateせずにイベントのやりとりができるので便利です!AppDelegate.swiftの存在はコード本体とは別の場所に あって馴染みずらい感覚が私にはあるのですが、このクラスでもっと親近感わきそうだと思いました、、、
↓こんな記事もありますよ!
![]() 2021-04-22 14:54:53 | かっこいいカルーセル厳選3点!WebGLを使った渋い画像スライダー。
昨日カッコいいスライダーを探す旅にでていました。
よく使われるswiper など、シンプルで使いやすくデザインも良いのですが、何かもの足りない、、、。
そこで調べていくと、webglを使... |
![]() 2021-04-20 14:46:43 | Twillioとは何??skywayとは何が違うのか。webrtcを使う際に判断する情報。
先日webrtc開発で「twillio」といった名前を聞きました。
以前自分でskywayを使ってwebrtcを試していた際に調べた時には、無かったような気もするんだけと、KDDIが絡んでいる... |
![]() 2021-04-19 13:58:21 | 先日久々にアップデートした妊娠週刊パパのAndroid版も。アカウント移管したいんだけど、注文ID見当たらないよ。
先日の妊娠、出産、育児関連のアプリ「妊娠週刊パパ」をAndroid studioで再リリースしようと思ってるんだけど、コンパイル失敗してエミュレータで動かせない。
多分sdkとかapiのバージ... |
Tweet
![]() |
|||
|