How to pass data using NotificationCenter in swift 3.0 and NSNotificationCenter in swift 2.0?

Swift 2.0 Pass info using userInfo which is a optional Dictionary of type [NSObject : AnyObject]? let imageDataDict:[String: UIImage] = [“image”: image] // Post a notification NSNotificationCenter.defaultCenter().postNotificationName(notificationName, object: nil, userInfo: imageDataDict) // Register to receive notification in your class NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.showSpinningWheel(_:)), name: notificationName, object: nil) // handle notification func showSpinningWheel(notification: NSNotification) { if let … Read more

How to receive NSNotifications from UIWebView embedded YouTube video playback

There are no documented notifications sent by the UIWebView embedded movie player. In fact, the closed implementation used within the UIWebView does differ from the public MPMoviePlayerController in many aspects (e.g. DRM). The most important classes used for playing video content within that UIWebView are called MPAVController and UIMoviePlayerController. The latter one makes the player … Read more

Move view with keyboard using Swift

Here is a solution, without handling the switch from one textField to another: override func viewDidLoad() { super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector(“keyboardWillShow:”), name: UIKeyboardWillShowNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector(“keyboardWillHide:”), name: UIKeyboardWillHideNotification, object: nil) } func keyboardWillShow(notification: NSNotification) { if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() { self.view.frame.origin.y -= keyboardSize.height } } func keyboardWillHide(notification: NSNotification) { self.view.frame.origin.y … Read more

NSNotificationCenter addObserver in Swift

Swift 4.0 & Xcode 9.0+: Send(Post) Notification: NotificationCenter.default.post(name: Notification.Name(“NotificationIdentifier”), object: nil) OR NotificationCenter.default.post(name: Notification.Name(“NotificationIdentifier”), object: nil, userInfo: [“Renish”:”Dadhaniya”]) Receive(Get) Notification: NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name(“NotificationIdentifier”), object: nil) Function-Method handler for received Notification: @objc func methodOfReceivedNotification(notification: Notification) {} Swift 3.0 & Xcode 8.0+: Send(Post) Notification: NotificationCenter.default.post(name: Notification.Name(“NotificationIdentifier”), object: nil) Receive(Get) Notification: NotificationCenter.default.addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(notification:)), name: Notification.Name(“NotificationIdentifier”), … Read more

Send and receive messages through NSNotificationCenter in Objective-C?

@implementation TestClass – (void) dealloc { // If you don’t remove yourself as an observer, the Notification Center // will continue to try and send notification objects to the deallocated // object. [[NSNotificationCenter defaultCenter] removeObserver:self]; [super dealloc]; } – (id) init { self = [super init]; if (!self) return nil; // Add this instance of … Read more