最近在研究一个蓝牙打印机,厂家网站上提供了一个IOS SDK,其中有个方法不知道怎么调用,以前完全没学过IOS,只会一丢丢java
现在已知有MyPeripheral.h、BLKWrite.h、ViewController.m,我想在ViewController.m中获取到didReceiveTransparentData里回传的数据,请大佬指点!MyPeripheral.h部分代码:@protocol MyPeripheralDelegate<NSObject>
@optional
- (void)MyPeripheral:(MyPeripheral *)peripheral didReceiveTransparentData:(NSData *)data;
@end
BLKWrite.h 部分代码:@interface BLKWrite : NSObject<MyPeripheralDelegate>
@property (nonatomic, strong) MyPeripheral *connectedPeripheral;
@property (nonatomic, assign) BOOL bWiFiMode; //YES: Wi-Fi模式;NO:蓝牙模式
@property (nonatomic, strong) NSString *serverIP;
@property (nonatomic, assign) int port;
+(BLKWrite*) Instance;-(void) writeTscData:(NSData*) data withResponse:(BOOL) flag;
-(void) writeEscData:(NSData*) data withResponse:(BOOL) flag;
-(BOOL) isConnecting;-(void) setPeripheral:(MyPeripheral*) peripheral;#pragma -Wi-Fi Mode-(void) initWiFiClient;-(int) PrintWidth;@end

解决方案 »

  1.   

    MyPeripheral有.m的文件吗?有的话贴出来看看
    既然有实例化这个类,应该不只有定义协议,还有其他的内容
      

  2.   

    MyPeripheral没有.m的文件
    项目结构如图
      

  3.   

    ViewController继承MyPeripheralDelegate,然后实现对应的函数,就能回调获取数据
      

  4.   

    是在ViewController的.m还是.h里写?我看到.h文件里是这样的@interface ViewController : UIViewController好像oc不支持多继承
    .m里是这样的
    @interface ViewController ()<UITextFieldDelegate>@property(nonatomic, weak) UITextField *mCurrentUITextField;
    typedef struct ARGBPixel{
        u_int8_t red;
        u_int8_t green;
        u_int8_t blue;
        u_int8_t alpha;
    }ARGBPixel;@end@implementation ViewController
      

  5.   

    遵守协议和多继承还是有点区别的,oc支持遵守多个协议,你可以直接在<UITextFieldDelegate>中直接添加MyPeripheralDelegate协议,用逗号隔开就行了
    <UITextFieldDelegate,MyPeripheralDelegate>
    然后再去实现一下协议中约定的didReceiveTransparentData方法.
    不过你要弄清楚是哪个对象需要调用这个协议方法,必须把viewController设置为他的代理
      

  6.   

    是不是只要遵守了协议,在任意的一个类里按这种格式书写代码就能被触发该方法?差不多是这样,不过还有很重要的一步就是需要代理来执行某个方法的类,必须指明代理是谁举个例子
    类A需要代理帮他实现一个方法doSomething(),于是类A定了一个协议,单单定协议还不够,类A
    还要有个属性用来指定代理是谁,代理可以是遵守了协议的任意对象
    Aprotocol
    {
        doSomething();
    }@interface A:NSObject
    @property (nonatomic,weak) id<Aprotocol> delegate; //声明代理
    @end然后在B类中你可以声明一下遵守协议
    @interfaceB<Aprotocol>
    @end然后实现协议中的方法
    @implementation
    -(void)doSomething{
        print('xxxx');
    }最后在A的实例那边指定b为他的代理就行了
    A *a = [[A alloc] init];
    a.delegate = b; //这一步很重要,不然a找不到代理