0

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

Hunter
  • 1,321
  • 4
  • 18
  • 34

2 Answers2

2

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)
Oleg Gordiichuk
  • 15,240
  • 7
  • 60
  • 100
  • ok cool but theres an error i cant fix on weak var delegate:TextViewControllerDelegate? it says weak may only be applyed to class and class-bound proticol type, not 'TestViewControllerDeleggate' and itll just keep saying that know matter where i put that line of code @Oleg Gordiichuk – Hunter Sep 05 '16 at 16:05
0

I fond the answer to my question

func mySelector(elem: AnyObject) {
    Extra1().see()
}

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.mySelector), name: "see", object: nil)

NSNotificationCenter.defaultCenter().postNotificationName("see", object: self)
Hunter
  • 1,321
  • 4
  • 18
  • 34