Hunter Hunter - 1 year ago 77
iOS Question

How to call a function from a different view controller with different classes

When you press on the screen im trying to get a function that Is in a different view Controller to be called usaully i would just do the
but because the view controller class that has the function im trying to call is a SCNView. it gives me this error with ns coder because in currently in a Skscene
enter image description here
the function im trying to call is in a class called Extra1 right below the viewdidload(not in it).

Update for comment below:


enter image description here

Answer Source

Small example for you:

Use delegate when connection is one to one.


Create protocol:

protocol TestViewControllerDelegate {
  func finishTask(sender: TestViewController)

Create reference for delegation. It should be weak it is important.

weak var delegate:TestViewControllerDelegate?

Exited you're class.

extension MainViewController: TestViewControllerDelegate {
  func finishTask(sender: TestViewController) {


Call delegate:

delegate?. finishTask(self)

Use notification when connection is one to many.


Add observer for notification.

 NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ClassName.test), name:"NotificationIdentifier", object: nil)

Post notification.

NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download