首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

我想从另一个ViewController调用我的函数

从另一个ViewController调用函数可以通过以下步骤实现:

  1. 首先,在目标ViewController中创建一个函数,确保该函数的访问级别是公开的(public)或者可以被其他类访问到。
  2. 在源ViewController中,确保你已经导入了目标ViewController的类文件。
  3. 在源ViewController中,创建一个目标ViewController的实例对象。
  4. 使用该实例对象调用目标ViewController中的函数。

下面是一个示例代码:

在目标ViewController中:

代码语言:txt
复制
public class TargetViewController: UIViewController {
    public func myFunction() {
        // 在这里编写你的函数逻辑
        print("调用了目标ViewController中的函数")
    }
}

在源ViewController中:

代码语言:txt
复制
import UIKit

class SourceViewController: UIViewController {
    func callFunctionInTargetViewController() {
        let targetVC = TargetViewController()
        targetVC.myFunction()
    }
}

这样,当你在源ViewController中调用callFunctionInTargetViewController函数时,它将创建一个目标ViewController的实例对象,并调用目标ViewController中的myFunction函数。

这种方式适用于在同一个应用程序中的不同视图控制器之间进行函数调用。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券