簡體   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