0


I have written the sending notification in one view and receiving on another view but it is not working. I have followed the code from NSNotificationCenter addObserver in Swift but couldn't make it, any suggestion will really be grateful

Here is my code

ViewController 1 or sending notification view

import UIKit

class HomeView: UIViewController {

@IBOutlet weak var subscribers: CustomButton!
@IBOutlet weak var newResig: CustomButton!


override func viewDidLoad() {

}

override func viewWillDisappear(animated: Bool) {

}

@IBAction func languageEnglish(sender: AnyObject) {
    subscribers.setTitle("SUBSCRIBERS", forState: .Normal)
    newResig.setTitle("NEW REGISTRATION", forState: .Normal)


}

@IBAction func languageArabic(sender: AnyObject) {
    subscribers.setTitle("المشتركين", forState: .Normal)
    newResig.setTitle("اشتراك جديد", forState: .Normal)

    NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)
}

}

ViewController 2 or receiving notification view

import UIKit

class Subscription: UIViewController {


@IBOutlet weak var scheduling_Appointment: UILabel!
@IBOutlet weak var deliveryMedicine: UILabel!
@IBOutlet weak var visitDoc: UILabel!
@IBOutlet weak var medicalHelp: UILabel!
@IBOutlet weak var ChronicDiseases: UILabel!
@IBOutlet weak var contactUs: UILabel!

override func viewDidLoad() {
    self.title = "Subscribers"
    self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()


    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(Subscription.methodOfReceivedNotification(_:)), name:"NotificationIdentifier", object: nil)

}

func methodOfReceivedNotification(notification: NSNotification){
    //Take Action on Notification
    scheduling_Appointment.text = "حجز كشف جديد"
    deliveryMedicine.text = "توصيل الادوي"
    visitDoc.text = " زيارة الاطباء"
    medicalHelp.text = "التحدث المباشر"
    ChronicDiseases.text = "الامراض المزمنة"
    contactUs.text = "اتصل بنا"
}

}
Community
  • 1
  • 1
Abdul Karim
  • 4,359
  • 1
  • 40
  • 55
  • Could you try swapping **#selector(Subscription.methodOfReceivedNotification(_:))** with just **"methodOfReceivedNotification:"**. This is how I normally do it and it works fine for me. – hannad May 05 '16 at 14:09
  • 1
    @hannad, syntax is ok for swift 3.0 compatibility. Problem is that, maybe, there is no any instance of class Subscription on navigation stack.. – Luca Davanzo May 05 '16 at 14:13
  • 3
    Do these view controllers exist simultaneously? The notification listener cannot respond to the post if it hasn't been instantiated yet. If you fire the IBAction in ViewControllerOne before you've instantiated ViewControllerTwo, the logic here will certainly not work The listener does not yet exist. – Dare May 05 '16 at 14:17
  • hand Sir, luca Davanzo Sir & Dare Sir thank you for your reply, I have tried using break point also and it never execute the applied function though the notification on first view and receiver on second view get called ... – Abdul Karim May 05 '16 at 14:42
  • 1
    @AbdulKarim your code looks good just that your SubscriptionController has no running instance in app.Try adding an observer on applicationdidifinishlaunch function in app delegate just for testing and let me know if it works.If it works i will explain you the answer – Ahmad Ishfaq May 05 '16 at 14:56
  • a/s @ahmadIshfaq sir as you said i have added the observer and the related func. and its called when the view is loaded – Abdul Karim May 05 '16 at 15:05
  • @AbdulKarim So the notification is received right?? – Ahmad Ishfaq May 05 '16 at 15:06
  • 1
    The issue isn't that adding the observer isn't called. The issue is that postNotificationName is being called before addObserver. If that happens, you miss the notification. It's posted before you start listening for it and therefore cannot act on it. – Dare May 05 '16 at 15:07
  • @dare Sir i have added a segue to navigate to another view and called the notification their but still the same. any suggestion will really be helpfull – Abdul Karim May 05 '16 at 15:14

0 Answers0