问题
I introduced “Factory Pattern” in my beacon scanning module. I referred http://crosbymichael.com/objective-c-design-patterns-factory.html
In my Factory class, 2 modes of beacons are switched between Interface classes “PCGoogleBeacon.h” and “PCAppleBeacon.h”.
//Header file of Factory
typedef enum beaconMode {
iBeacon,
Eddystone
} BeaconMode;
@interface PCBeaconFinder : NSObject
+(id) searchForBeaconMode:(BeaconMode) beaconMode;
@end
//Implementation of Factory
+(id) searchForBeaconMode:(BeaconMode) beaconMode
{
switch (beaconMode ) {
case iBeacon:
return [PCAppleBeacon new];
break;
case Eddystone:
return [PCGoogleBeacon new];
break;
default: NSLog(@"UNKOWN BEACON MODE");
}
}
In my Implementation file for interface classes.
//Header file
@protocol PCGetBeacon <NSObject>
-(void) scanBeaconsWithUUID:(NSString *) beaconId;
@end
//In the implementation file. — Implementation of Mode 1
#import "PCAppleBeacon.h"
@implementation PCAppleBeacon
-(void) scanBeaconsWithUUID:(NSString *) beaconId {
self.proximityContentManager = [[ProximityContentManager alloc]
initWithBeaconIDs:@[
[[BeaconID alloc] initWithUUIDString:beaconId major:0 minor:0]
]
beaconContentFactory:[EstimoteCloudBeaconDetailsFactory new]];
self.proximityContentManager.delegate = self;
[self.proximityContentManager startContentUpdates];
NSLog(@"----------- > iBeacon Implementation Called ");
}
//iBeacon Delegates goes here …
@end
// In the same above file— Implementation of Mode 2
#import "PCGoogleBeacon.h"
@implementation PCGoogleBeacon
-(void) scanBeaconsWithUUID:(NSString *) beaconId {
_scanner.delegate = self;
[_scanner startScanning];
NSLog(@"----------- > EDDYSTONE Implementation Called ");
}
//EDDYSTONE Delegates goes here …
@end
Everything is fine. Able to switch as from MainController,
id beaconFinderObject = [PCBeaconFinder searchForBeaconMode:iBeacon]; //or ‘Eddystone’ for Google beacon interface.
[beaconFinderObject scanBeaconsWithUUID:@"B0702880-A295-A8AB-F734-031A98A512DE"];
But why the delegates of corresponding classes are not called. ?
Note: Beacons are in the range.
回答1:
After creating "Shared Instance" for both PCAppleBeacon
and PCGoogleBeacon
class solved my problem. :-)
Explanation : Delegate methods of above classes are not called because 2 times those instantiated. First time instantiated in 'Implementation of Factory Class' along with setting it's delegates. Second time from the class Main view controller, where it's protocols are not implemented, hence receiver fails.
来源:https://stackoverflow.com/questions/35362923/factory-pattern-ibeacon-delegates-are-not-called-from-implemention-file