[英]How to call method from another class in Swift 4.0
I am working an an app that involves a wheel. 我正在开发一个涉及车轮的应用程序。 I want to execute a function in another class when the angular velocity reaches 0 after being spun.
我想在旋转后角速度达到0时在另一个类中执行一个函数。
The setup is that I have a SKView embedded in a view controller. 设置是我将SKView嵌入在视图控制器中。 I have a class called WheelView that is tied to that SKView.
我有一个名为WheelView的类,该类与该SKView关联。
Full class: https://github.com/swiftishard/wheelpractice2/blob/master/WheelView.swift This is the relevant portion of code. 完整的类: https : //github.com/swiftishard/wheelpractice2/blob/master/WheelView.swift这是代码的相关部分。
class WheelDelegate: NSObject, SKSceneDelegate {
func didSimulatePhysics(for scene: SKScene) {
guard let wheelScene = scene as? WheelScene else { return }
if (wheelScene.wheel.physicsBody?.angularVelocity ?? 0.0) > 0.0 {
print(wheelScene.wheel.physicsBody?.angularVelocity)
if(wheelScene.wheel.physicsBody?.angularVelocity ?? 0.0) < 25.0 {
wheelScene.wheel.physicsBody?.angularVelocity = 0.0
if(wheelScene.wheel.physicsBody?.angularVelocity ?? 0.0) == 0.0 {
print("CALL METHOD FROM ANOTHER VIEW CONTROLLER")
}
}
}
}
} }
Main view controller where method to call is located 调用方法所在的主视图控制器
import UIKit
class Main: UIViewController {
func methodToCall() {
print("METHOD CALLED")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
So how would I call methodToCall
from the didSimulatePhysics code? 那么我该如何从didSimulatePhysics代码中调用
methodToCall
?
I'm assuming WheelDelegate
is created within Main
as an object. 我假设
WheelDelegate
在Main
作为对象创建。
Multiple ways you can do this: 您可以通过多种方式执行此操作:
1) Most common way I have seen is to create a protocol as well as some delegates. 1)我见过的最常见的方法是创建一个协议以及一些委托。
Inside Main
class, right below all the imports, you can do. 在
Main
类内部,就在所有导入的下面,您可以执行此操作。
protocol ViewControllerTalkingDelegate {
func methodToCall()
}
Inside WheelDelegate
you add this as a global variable 在
WheelDelegate
内部,将其添加为全局变量
var delegate:ViewControllerTalkingDelegate?
Then, whenever you create WheelDelegate
inside Main
然后,只要在
Main
内部创建WheelDelegate
let wheelDelegate = WheelDelegate()
wheelDelegate.delegate = self
Then, inside Main
at the bottom, you can do 然后,在底部的
Main
内部,您可以执行
extension Main:ViewControllerTalkingDelegate {
func methodToCall() {
//Do Something
}
}
Now, inside WheelDelegate
you can do delegate.methodToCall()
. 现在,在
WheelDelegate
内部,您可以执行delegate.methodToCall()
WheelDelegate
delegate.methodToCall()
。
2) Other way I have seen is to pass the 1st class as a variable to the 2nd class. 2)我看到的另一种方法是将第一类作为变量传递给第二类。
Inside WheelDelegate add a global variable 在WheelDelegate内部添加全局变量
var myViewController:Main?
You can then either take in Main
as a parameter when initializing or when you create WheelDelegate
you can do 然后,您可以在初始化时将
Main
作为参数,或者在创建WheelDelegate
时进行操作
var wheelDelegate = WheelDelegate()
wheelDelegate.myViewController = self
Then inside WheelDelegate
you can do 然后在
WheelDelegate
您可以执行
self.myViewController.methodToCall()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.