I have 3 classes in my project : HeliController, FlightView and ConnectionView. HeliController stores some connection data which is to be used by both FlightView and ConnectionView.
In both ConnectionView and HeliController I have
// Interface
#import "HeliController.h"
@property (retain) HeliController *heliController;
// Implementation
@synthesize HeliController = _HeliController
The ConnectionView takes care of all connection methods, so this class receives the peripheral I am communication with. From here, I send the peripheral data to be stored in the HeliController (which also is the delegate for the peripheral class):
// 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;
}
Now I can reach the peripheral from ConnectionView with
self.heliController.peripheral
and see that both have addresses on stack:
_heliController HeliController * 0x0017f9e0
_peripheral CBPeripheral * 0x0017e570
From FlightView I also want to reach the peripheral data. I do
self.heliController = [[HeliController alloc] init];
and see in the debugger that self.heliController gets an address on stack
_heliController HeliController * 0x0018ac60
but the peripheral is nil
_peripheral CBPeripheral * 0x00000000
why is this? What am I forgetting? This problem occured when I had to restructure my application, and I can't figure out what I have done wrong.
My sollution was to send the heliController object to the FlightView when I changed between the views like this:
- (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];
}