Hunter Hunter - 3 months ago 8
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
viewControllerFuncIsIn().NameOfFunc()
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:

Extra1(coder:NSCoder())!.see()


enter image description here

Answer

Small example for you:

Use delegate when connection is one to one.

Delegate:

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.

Notification:

Add observer for notification.

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

Post notification.

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