如果您想在多个ViewController中处理远程iBeacon数据,您可以在AppDelegate中设置范围,然后从那里调用每个ViewController上的公共方法。主要的didRangeBeacons:inRegion:在你的AppDelegate中,如果ViewController被激活,可以调用每个ViewController的自定义方法。
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
if (self.firstViewController != Nil) {
[self.firstViewController handleBeacons: beacons];
}
if (self.secondViewController != Nil) {
[self.secondViewController handleBeacons: beacons];
}
}
为了做到这一点如上,你必须保持你的AppDelegate中为每个视图控制器属性:
@interface MyAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) FirstViewController *firstViewController;
@property (strong, nonatomic) SecondViewController *secondViewController;
@end
您可以从每个视图控制器填充它们是这样的:
- (void)viewDidLoad {
[super viewDidLoad];
MyAppDelegate *appDelegate = (MyAppDelegate) [[UIApplication sharedApplication] delegate];
appDelegate.firstViewController = self;
}