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の存在はコード本体とは別の場所に あって馴染みずらい感覚が私にはあるのですが、このクラスでもっと親近感わきそうだと思いました、、、
↓こんな記事もありますよ!
![]() | Facebook SDKを試してみる#3(再調査)SDKのインストーラが起動しても結局frameworkをドラッグドロップしなちゃ追加できない、面倒臭い、と発言してしまったのですが、 どうやらSDKをインストーラにてインストールした場合(SDK4.1)は、importするだけでどうやらSDKが追加できるとかなんとか。 |
![]() | SQLiteを使いたい。ラッパーを検証してみる。(#2)前回の記事でSQLiteのラッパのRMDB, SWiftData,SQLite.swiftの検証をトライしまいたが、時間の問題で頓挫してしまいましたので続きです。 。ある程度準備が完了しましたので、それぞれのサンプルを動かしてみてみようと思います。InsertとSelectとインストール感を比較してみます。 |
![]() | AppleWatchをNSFileCoordinatorとKeychainでデータ交換前回の記事(ナターシャさんのサンプルデモを元に解説)の続きです。今記事はNSUserDefaultではなく、 NSFileCoordinatorとKeychain sharingを利用しての解説です。ナターシャさんのサンプルに関して前記事をご参照ください。 |
Tweet
![]() |
|||
|