从另一个ViewController调用函数可以通过以下步骤实现:
下面是一个示例代码:
在目标ViewController中:
public class TargetViewController: UIViewController {
public func myFunction() {
// 在这里编写你的函数逻辑
print("调用了目标ViewController中的函数")
}
}
在源ViewController中:
import UIKit
class SourceViewController: UIViewController {
func callFunctionInTargetViewController() {
let targetVC = TargetViewController()
targetVC.myFunction()
}
}
这样,当你在源ViewController中调用callFunctionInTargetViewController
函数时,它将创建一个目标ViewController的实例对象,并调用目标ViewController中的myFunction
函数。
这种方式适用于在同一个应用程序中的不同视图控制器之间进行函数调用。
领取专属 10元无门槛券
手把手带您无忧上云