0

我在我的信标扫描模块中引入了“<strong>Factory Pattern”。我提到了 http://crosbymichael.com/objective-c-design-patterns-factory.html

在我的工厂类中,两种信标模式在接口类“<strong>PCGoogleBeacon.h”和“<strong>PCAppleBeacon.h”之间切换。

//工厂的头文件

typedef enum beaconMode {
    iBeacon,
    Eddystone
} BeaconMode;

@interface PCBeaconFinder : NSObject

+(id) searchForBeaconMode:(BeaconMode) beaconMode;

@end

//工厂的实现

+(id) searchForBeaconMode:(BeaconMode) beaconMode
{

    switch (beaconMode ) {

        case iBeacon:

            return   [PCAppleBeacon new];

            break;

        case Eddystone:

            return   [PCGoogleBeacon new];

            break;

        default: NSLog(@"UNKOWN BEACON MODE");


    }

}

在我的接口类的实现文件中。

//Header file

@protocol PCGetBeacon <NSObject>

-(void) scanBeaconsWithUUID:(NSString *) beaconId;

@end

//在实现文件中。— 模式一的实施

#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

// 在同一个文件中——模式2的实现

#import "PCGoogleBeacon.h"

@implementation PCGoogleBeacon

-(void) scanBeaconsWithUUID:(NSString *) beaconId {

    _scanner.delegate = self;

    [_scanner startScanning];

    NSLog(@"----------- > EDDYSTONE  Implementation Called ");

}

//EDDYSTONE Delegates goes here …

@end

一切安好。能够从 MainController 切换,

 id beaconFinderObject =   [PCBeaconFinder searchForBeaconMode:iBeacon];  //or ‘Eddystone’ for Google beacon interface.

 [beaconFinderObject scanBeaconsWithUUID:@"B0702880-A295-A8AB-F734-031A98A512DE"];

但是为什么不调用相应类的代表。?

注意:信标在范围内。

4

1 回答 1

0

在为两者创建“共享实例”后PCAppleBeaconPCGoogleBeacon班级解决了我的问题。:-)

解释:上述类的委托方法没有被调用,因为它们实例化了 2 倍。第一次在“工厂类的实现”中实例化并设置它的委托。第二次来自主视图控制器类,它的协议没有实现,因此接收器失败。

于 2016-02-29T04:16:24.983 回答