繁体   English   中英

如何从AppDelegate中的ViewController调用函数?

[英]How does one call a function from ViewController in AppDelegate?

我正在制作一个使用iBeacon的应用程序。 当检测到iBeacon时(在AppDelegate内部),该应用程序将检测手机的坐标并将其转发给API。

我在ViewController中设置了一个名为runGPS()的函数,该函数可以执行上述操作,并试图在检测到iBeacon时使其执行。

当我执行ViewController()。runGPS()时,出现错误:调用中缺少“编码器”的参数

我已经查找了此问题,并一直在圈中解决此问题。 一种纠正会导致另一种错误等,等等。

如何纠正这个错误?或者我的总体策略是错误的?

func locationManager(manager: CLLocationManager,
    didEnterRegion region: CLRegion) {
        manager.startRangingBeaconsInRegion(region as! CLBeaconRegion)
        manager.startUpdatingLocation()

        NSLog("You entered the region")

        ViewController(NSCoder)!.runGPS()

}

我还在我的应用程序中实现了iBeacon。 您必须在该viewcontroller中添加此位置管理器委托,并在其中设置委托,例如

locationManager.delegate = self
locationManager.requestAlwaysAuthorization()

并且您可以在viewController中检查是否有任何用户输入到您的信标区域,然后将运行此委托方法

 func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {

    }

您可以在参数中收到的信数组中检查信

如果不打算将runGPS()代码从ViewController类中分离出来,则应将其分开。 它使用ViewController的任何功能吗? 如果不是,则应将其移至用于将数据传递到API的单独的类。 如果您不能这样做,则可以将runGPS声明为静态方法,并称为ViewController.runGPS()前提是您未在runGPS()方法中使用ViewController任何属性runGPS()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM