Я наткнулся на этот пост, пытаясь ответить на тот же точный вопрос. На всякий случай, если кто-то еще ищет, я наконец-то понял, как это сделать с NSNotificationCenter. По сути, NSNotificationCenter отправляет «широковещательные» сообщения всему приложению. Если «наблюдатель» слушает, как вы можете видеть ниже, вызывается данный метод. Код выглядит так:
В вашем приложении Делегат:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];
// Make sure you add this so that your tab bar calls its delegate methods
tabBarController.delegate = self;
}
// Optional UITabBarControllerDelegate method (this will be commented out by default - uncomment it)
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
// Create the NSNotificationCenter
[[NSNotificationCenter defaultCenter] postNotificationName:@"tabChanged" object:nil];
}
На ваш взгляд контроллер:
- (void)viewDidLoad {
[super viewDidLoad];
// Register an observer to stop audio recording/playing on tab change
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(tabChanged)
name:@"tabChanged"
object:nil];
}
- (void)tabChanged {
@"Received Notification!";
if([player isPlaying]) {
[player stop];
}
}