Dans le Follwoing ViewControllerClass, j'obtiens EXC_BAD_ACCESS lorsque j'essaie d'appeler presentModalViewController dans la méthode ViewDidAppear.Chargement de PresentModalViewController dans ViewDidAppear provoque EXC_BAD_ACCESS
#import "SounderViewController.h"
#import "ASIFormDataRequest.h"
#import "ASIHTTPRequest.h"
#import "JSON.h"
#import "InfoViewController.h"
@implementation SounderViewController
@synthesize ipod;
@synthesize ivc;
@synthesize title_lb, artist_lb, check;
-(IBAction)showCurrentSongInfo{
MPMediaItem * song = [ipod nowPlayingItem];
NSString * title = [song valueForProperty:MPMediaItemPropertyTitle];
NSString * artist = [song valueForProperty:MPMediaItemPropertyArtist];
title_lb.text = title;
artist_lb.text = artist;
}
-(void)playbackStateChanged: (NSNotification*) notification{
[self showCurrentSongInfo];
NSLog(@"Playback state: %@",[notification name]);
if (ipod.playbackState != MPMusicPlaybackStatePlaying) {
NSLog(@"Is not playing");
[self presentModalViewController:self.ivc animated:YES];
}else if (ipod.playbackState == MPMusicPlaybackStatePlaying) {
NSLog(@"Is playing");
[self dismissModalViewControllerAnimated:YES];
}
}
-(void)nowPlayingItemChanged: (NSNotification*) notification{
[self showCurrentSongInfo];
NSLog(@"Playing item changed: %@",[notification name]);
}
- (void)viewDidLoad {
[super viewDidLoad];
self.ivc = [[InfoViewController alloc] initWithNibName:@"InfoViewController" bundle:nil];
self.ipod = [MPMusicPlayerController iPodMusicPlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector (playbackStateChanged:)
name:@"MPMusicPlayerControllerPlaybackStateDidChangeNotification"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector (nowPlayingItemChanged:)
name:@"MPMusicPlayerControllerNowPlayingItemDidChangeNotification"
object:nil];
[[MPMusicPlayerController iPodMusicPlayer] beginGeneratingPlaybackNotifications];
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
if (ipod.playbackState != MPMusicPlaybackStatePlaying) {
[self presentModalViewController:self.ivc animated:YES];
}else{
[self showCurrentSongInfo];
}
}
-(IBAction)showInfoView{
[self presentModalViewController:self.ivc animated:YES];
}
#pragma mark View Methods
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
appel de méthode
[self presentModalViewController:self.ivc animated:YES];
en ViewDidAppear provoque EXC_BAD_ACCESS.
J'ai essayé de le déboguer avec NSZombieEnabled mais j'ai reçu seulement un appel de pile à main. La chose qui me rend fou est que si le même code est exécuté depuis la méthode playbackStateChanged, cela fonctionne très bien.
Si l'un de vous peut aider, je ne deviendrai pas aussi rapide. Merci.
Avez-vous déclaré ivc (pas un bon nom de propriété) avec l'attribut retain ou copy? Si ce n'est pas le cas, il n'est pas conservé en quittant loadView, et ce serait votre problème. – Felixyz
Oui, j'ai déclaré ivc avec la propriété reatain: @property (nonatomic, retain) InfoViewController * ivc; – Cyprian