1

私のプロジェクトには、HeliController、FlightView、および ConnectionView の 3 つのクラスがあります。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 に答える