ios - 如何从具有不同类的不同 View Controller 调用函数

标签 ios swift xcode nscoder

当你在屏幕上按下时,我试图让一个位于不同 View Controller 中的函数被称为usaully,我只会做 viewControllerFuncIsIn().NameOfFunc() 但是因为具有我试图调用的函数的 View Controller 类是 SCNView。它给了我这个 ns coder 的错误,因为目前在 Skscene 中 enter image description here 我试图调用的函数位于 viewdidload 正下方的名为 Extra1 的类中(不在其中)。

更新以下评论:

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

enter image description here

最佳答案

给你的小例子:

当连接是一对一时使用委托(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/

相关文章:

arrays - 如何在swift中对类型化数组进行排序?

iphone - 从另一个 View Controller 返回时返回 ScrollView 中的旧位置 swift iOS 8

ios - spinnerView 无法在 appdelegate 中工作

ios - 在消息框中添加图像(UIAlertController)

ios - iPhone : touches on UIViewController

ios - 在 Swift 中从另一个 viewController 的类访问属性?

ios - UIAccessibilityCustomAction 选择器不适用于静态方法

swift - TextView 动态高度和 y 坐标不起作用

xcode - 发布预编译的CocoaPods

iphone - iPad 模拟器 (4.3) 崩溃并出现 dyld 错误