You shouldn't access your outlets from anywhere else but from the view controller that owns them. Also, you'd want do make your outlets weak
.
To solve your problem you could use notifications:
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray *)requests
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyAppDidReceiveWriteRequestNotification"
object:nil
userInfo:@{@"text": @"text changed", @"switchOn": @(NO)}];
}
Then, in your FirstViewController
, you register yourself as an observer for that notification, for example in viewDidLoad
:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleDidReceiveWriteRequestNotification:)
name:@"MyAppDidReceiveWriteRequestNotification"
object:nil];
}
And you implement the handler:
- (void)handleDidReceiveWriteRequestNotification:(NSNotification *)note
{
[self.labelTest setText:note.userInfo[@"text"]];
[self.mySwitch setOn:[note.userInfo[@"switchOn"] boolValue] animated:YES];
}
And you clean up:
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
That's it. Much cleaner.
Also, you'd want to turn the name of the notification and the names of the userInfo dictionary keys into constants so that you don't have the same string literal in multiple places in your code.