在 AppDelegate 中我有下一个扩展:
extension AppDelegate: CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager!,didRangeBeacons beacons: [AnyObject]!, inRegion region: CLBeaconRegion!) {
let viewController:ViewController = window!.rootViewController as! ViewController
viewController.beacons = beacons as! [CLBeacon]?
NSLog("didRangeBeacons");
}
}
在 ViewController 中我这样做:
class ViewController: UIViewController {
var beacons: [CLBeacon]?
override func viewDidLoad() {
super.viewDidLoad()
if(beacons != nil){
println(beacons![0])
}
}
}
它没有给我打印任何东西。但它不是空的,是一个数组。它在日志 didRangeBeacons
中打印。那么,问题是什么以及如何访问它?
最佳答案
CLLocationManagerDelegate
的方法 locationManager(manager:,didRangeBeacons beacons:, inRegion:)
是一个异步调用。在这里,您尝试通过 viewDidLoad
方法获取 ViewController
中的 beacons
的值。
AppDelegate 上的方法可能会在 View Controller 的 viewDidLoad 执行之后触发。
检查 viewDidLoad
是否在 locationManager(manager:,didRangeBeacons beacons:, inRegion:)
关于ios - 无法在 ViewController 的 AppDelegate 中使用数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30861152/