通过多个ViewControllers访问用户定义的委托 - iOS

时间:2016-02-15 00:00:15

标签: ios delegates protocols

我正在使用某些硬件附带的BLE框架文件,但我只能在第一个视图控制器中调用这些函数。我的应用程序有两个视图控制器,我打算使用BLEDelegate在两个视图中编写和读取BLE数据。

第一个视图控制器中的所有内容都按预期工作,但我没有从第二个视图控制器中的BLE文件中获取任何操作。

这是我项目的骨架。

BLE.h

% some image data
img = rand(64);

% define variables
imgsize = 500;  % size on screen
xreal = 1;      % meter
yreal = 1;      % meter

% create square figure
figure('Position',[0,0,imgsize,imgsize]);

% pixel axes and actual plot
a=axes('Position',[.2 .2 .7 .7]);
set(a,'Units','normalized');
iptsetpref('ImshowAxesVisible','on');
imshow(img,'Parent',a);

% real world axis (below)
b=axes('Position',[.2 .1 .7 1e-12]);
set(b,'Units','normalized');
set(b,'Color','none');
set(b,'xlim',[0 xreal]);

% real world axis (left)
c=axes('Position',[.1 .2 1e-12 .7 ]);
set(c,'Units','normalized');
set(c,'Color','none');
set(c,'ylim',[0 yreal],'YDir','reverse');

% set labels
xlabel(a,'Pixels')
xlabel(b,'Real distance (m)')
ylabel(a,'Pixels');
ylabel(c,'Real distance (m)');
title(a,'A nice title to this noisy image');

BLE.m

#import <CoreBluetooth/CoreBluetooth.h>
@protocol BLEDelegate
@optional
-(void) bleDidReceiveData:(unsigned char *) data length:(int) length;
@required
@end
@interface BLE : NSObject;
@property (nonatomic,assign) id <BLEDelegate> delegate;
-(void) write:(NSData *)d;
@end

ViewController.h

#import "BLE.h"
@interface BLE ()
@end
@implementation BLE
@synthesize delegate;
-(void) write:(NSData *)d
{
//send BLE packet
NSData *data = [NSData dataWithBytes: &d length: sizeof(d)];
NSLog(@"%@", data);
}
@end

ViewController.m

#import <UIKit/UIKit.h>
#import "BLE.h"
@interface ViewController : UIViewController<BLEDelegate>
@property (nonatomic, strong) BLE *ble;
@property (weak, nonatomic) IBOutlet UIButton *btnClicked;
@end

SecondViewController.h

#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize ble;
- (void)viewDidLoad {
  [super viewDidLoad];
  ble = [[BLE alloc] init];
  ble.delegate = self;
}
-(void)bleDidReceiveData:(unsigned char *)data length:(int)length
{
  //parse data
}
- (IBAction)btnClicked:(id)sender
{
  uint32_t packet = 0xFFFF;
  NSData *data = [NSData dataWithBytes: &packet length: sizeof(packet)];
  [ble write:data];
}
@end

SecondViewController.m

#import <UIKit/UIKit.h>
#import "BLE.h"
@interface SecondViewController : UIViewController<BLEDelegate>
@property (nonatomic, strong) BLE *ble;
@property (weak, nonatomic) IBOutlet UIButton *btnClicked;
@end

0 个答案:

没有答案