如何从子项目中调用函数

时间:2018-09-12 15:14:27

标签: ios swift xcode

我想在我的主要Xcode项目上按下UIButton,并从我的项目列表中运行一个子项目。 这可能吗 ? 这是我从MainViewController到我的主项目中的代码:

import UIKit

class MainViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }
    @IBAction func playSubproject(_ sender: UIButton) {

        // here I want to run the SubProject O2App.xcodeproj
        // More exactly I want to run a method from that subproject which is in ViewController.swift and is named startUnity().
    }
}

这是我的ViewController子项目中的代码:

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var backgroundImageView: UIImageView!


    override func viewDidLoad() {
        super.viewDidLoad()
        backgroundImageView.isHidden = false
        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
            appDelegate.startUnity()

            NotificationCenter.default.addObserver(self, selector: #selector(handleUnityReady), name: NSNotification.Name("UnityReady"), object: nil)
            NotificationCenter.default.addObserver(self, selector: #selector(handleUnityPlayHologram(_:)), name: NSNotification.Name("UnityPlayHologram"), object: nil)
        }
    }
}

这是子项目中的AppDelegate,我具有功能startUnity。如果可能的话,我想将此startUnity函数调用到我的Main Project中。这是代码:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    var application: UIApplication?

    @objc var currentUnityController: UnityAppController!

    var isUnityRunning = false

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        self.application = application
        unity_init(CommandLine.argc, CommandLine.unsafeArgv)

        currentUnityController = SVFUnityPluginAppController()
        currentUnityController.application(application, didFinishLaunchingWithOptions: launchOptions)

        // first call to startUnity will do some init stuff, so just call it here and directly stop it again
        startUnity()
        stopUnity()

        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.

        if isUnityRunning {
            currentUnityController.applicationWillResignActive(application)
        }
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

        if isUnityRunning {
            currentUnityController.applicationDidEnterBackground(application)
        }
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.

        if isUnityRunning {
            currentUnityController.applicationWillEnterForeground(application)
        }
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

        if isUnityRunning {
            currentUnityController.applicationDidBecomeActive(application)
        }
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

        if isUnityRunning {
            currentUnityController.applicationWillTerminate(application)
        }
    }

    public func startUnity() {
        if !isUnityRunning {
            isUnityRunning = true
            currentUnityController.applicationDidBecomeActive(application!)
        }
    }

    public func stopUnity() {
        if isUnityRunning {
            currentUnityController.applicationWillResignActive(application!)
            isUnityRunning = false
        }
    }
}

这是我的主项目和子项目的屏幕截图:

Structure

编辑:

在MainProject中,进入“构建设置”,然后在“标题搜索路径”中使用以下代码行设置SubProject中所有类的路径:$(继承)“ $(SRCROOT)/ O2App​​ / O2App​​ / Unity”“ $(SRCROOT)/ O2App​​ / O2App​​ / Unity / Classes“” $(SRCROOT)/ O2App​​ / O2App​​ / Unity / Classes / Unity“” $(SRCROOT)/ O2App​​ / O2App​​ / Unity / Classes / Native“” $(SRCROOT) / O2App​​ / O2App​​ / Unity / Libraries“” $(SRCROOT)/ O2App​​ / O2App​​ / Unity / Libraries / libil2cpp / include“。现在在MainViewController中,我导入O2App​​,现在我可以访问所有这些类。但是,当我尝试从该类中调用方法时,会给我一个错误。 这是我在按下按钮时试图在功能中执行的操作:

@IBAction func playSubproject(_ sender: UIButton) {
        ViewController().viewDidLoad()
    }

这是错误:

Undefined symbols for architecture arm64:
  "type metadata accessor for O2App.ViewController", referenced from:
      RGA_MainProject.MainViewController.playSubproject(__ObjC.UIButton) -> () in MainViewController.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

0 个答案:

没有答案