Je suis tombé sur ce poste en essayant de répondre à la même question exacte. Juste au cas où quelqu'un d'autre cherche encore, j'ai finalement compris comment le faire avec un NSNotificationCenter. Fondamentalement, un NSNotificationCenter envoie des "diffusions" un message à l'ensemble de l'application. Si un "observateur" arrive à écouter, comme vous pouvez le voir ci-dessous, une méthode donnée est appelée. Le code ressemble à ceci:
Dans votre délégué App:
- (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];
}
Dans votre contrôleur de vue:
- (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];
}
}
viewDidDisappear: n'est pas appelé par UITabBarController malheureusement. – millenomi
ahh - bien dans ce cas vous pourriez facilement appeler votre UITabBarControllerDelegate ces méthodes et encapsuler la logique de quoi faire sur un changement d'onglet dans chaque contrôleur. –
Merci pour l'aide, mais je ne peux pas le faire fonctionner. Je ne peux pas obtenir @interface dans UITabBarControllerDelegate.h pour accepter. –