如果您想在多個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;
}