0

I want to create chat using apple notification and I want when I get the notification pass the data to my Table View to update it I am trying to do a sample example to test how to pass data form app delegate to viewController and it not working can anyone help please My ViewController Code

class ViewController: UIViewController {

  var fileDirectory: String = String()
  let delegate = UIApplication.sharedApplication().delegate as! AppDelegate

  override func viewDidLoad() {
      super.viewDidLoad()
      NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.refreshView(_:)), name: "refreshView", object: nil)
  }

  func refreshView(notification: NSNotification) {
      fileDirectory = delegate.filePath
      print("tester")
  }

  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
  }
}

I want to access refreshView function from appDelegate

zurfyx
  • 31,043
  • 20
  • 111
  • 145
Mohamed Atef
  • 169
  • 3
  • 16
  • Is there a specific reason for using notification center? If this is the only view controller needing to know when the notification comes in, I would update the view controller directly. I answered a very similar question ages ago: http://stackoverflow.com/a/36443812/2237587 – Simon Feb 02 '17 at 08:54
  • not working man Iam trying to print "Away" and not working too it no go to the if {} i tried your both ways and both no working does i need to make anything in view controller class ? if let rootViewController = window?.rootViewController as? ViewController { print("Away") rootViewController.TestNotificationPasse("TesTef") } – Mohamed Atef Feb 02 '17 at 13:26

1 Answers1

4

You wrote below line is right in ViewController in viewDidLoad()

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.refreshView), name: "AnyThingYouWant", object: nil)
}

But Add this line in appDelegate where you want to call ViewController.refreshView method

NotificationCenter.default.post(name: "AnyThingYouWant", object: self)
}

if you want to call refreshView method when got notification then write above line in didReceiveLocal/RemoteNotification Method

Sunil Prajapati
  • 473
  • 5
  • 17