当你在屏幕上按下时,我试图让一个位于不同 View Controller 中的函数被称为usaully,我只会做 viewControllerFuncIsIn().NameOfFunc() 但是因为具有我试图调用的函数的 View Controller 类是 SCNView。它给了我这个 ns coder 的错误,因为目前在 Skscene 中 我试图调用的函数位于 viewdidload 正下方的名为 Extra1 的类中(不在其中)。
更新以下评论:
Extra1(coder:NSCoder())!.see()
最佳答案
给你的小例子:
当连接是一对一时使用委托(delegate)。
代表:
创建协议(protocol):
protocol TestViewControllerDelegate {
func finishTask(sender: TestViewController)
}
为委托(delegate)创建引用。它应该很弱很重要。
weak var delegate:TestViewControllerDelegate?
下课了。
extension MainViewController: TestViewControllerDelegate {
func finishTask(sender: TestViewController) {
}
}
调用委托(delegate):
delegate?. finishTask(self)
当连接是一对多时使用通知。
通知:
为通知添加观察者。
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ClassName.test), name:"NotificationIdentifier", object: nil)
发布通知。
NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)
关于ios - 如何从具有不同类的不同 View Controller 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39332821/