1

我的项目中有 3 个类:HeliController、FlightView 和 ConnectionView。HeliController 存储一些连接数据,供 FlightView 和 ConnectionView 使用。

在 ConnectionView 和 HeliController 我都有

// Interface
#import "HeliController.h"
@property (retain) HeliController *heliController;

// Implementation
@synthesize HeliController = _HeliController

ConnectionView 负责所有连接方法,因此此类接收我正在与之通信的外围设备。从这里,我发送要存储在 HeliController 中的外围数据(这也是外围类的委托):

// ConnectionView.m
self.heliController = [[HeliController alloc] initWithDelegateAndPeripheral:self peripheral:peripheral]; 
// Self will receive callbacks from HeliController and the connected peripheral is stored in the HeliController

// HeliController.m:
- (id)initWithDelegateAndPeripheral:(id<HeliControllerDelegate>)delegate peripheral:(CBPeripheral *)peripheral{

if([super init]){
    self.peripheral = peripheral;
    self.peripheral.delegate = self;
}
return self;

}

现在我可以从 ConnectionView 到达外围设备

self.heliController.peripheral

并看到两者都有堆栈地址:

_heliController HeliController *    0x0017f9e0
_peripheral CBPeripheral *  0x0017e570


从 FlightView 我也想获得外围数据。我愿意

self.heliController = [[HeliController alloc] init];

并在调试器中看到 self.heliController 在堆栈上获取地址

_heliController HeliController *    0x0018ac60

但外围为零

_peripheral CBPeripheral *  0x00000000

为什么是这样?我忘记了什么?当我不得不重组我的应用程序时出现了这个问题,我无法弄清楚我做错了什么。

4

1 回答 1

0

当我在这样的视图之间进行更改时,我的解决方案是将 heliController 对象发送到 FlightView:

- (IBAction)changeViewToFlight:(id)sender {

FlightView *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"FlightViewIdentifier"];

// Make sure the heliController object is passed on to the FlightView
vc.heliController = self.heliController;

[self.navigationController pushViewController:vc animated:YES];
}
于 2012-07-18T11:22:19.683 回答