Factory pattern : iBeacon Delegates are not called from implemention file

限于喜欢 提交于 2019-12-08 14:46:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!