2010-02-19 12 views
2

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.

+0

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

+0

Oui, j'ai déclaré ivc avec la propriété reatain: @property (nonatomic, retain) InfoViewController * ivc; – Cyprian

Répondre

2

Je l'ai finalement fait fonctionner! Mais je pense que c'est juste une solution rapide.

Je trouve que faire mon IVC pour montrer que je dois retarder l'appel à presentModalViewController

[self performSelector:@selector(showWaitingMessageView:) withObject:self.ivc afterDelay:1]; 

Voilà. Ça marche. Je ne sais pas pourquoi cela a aidé, alors si l'un de vous les gourous en sait plus à ce sujet s'il vous plaît éclairer moi.

+0

J'ai le même problème, il se bloque sur mon iPhone 2G iOS 3.1.2 et ne plante pas dans le simulateur. Je suppose que la raison pour laquelle cette solution fonctionne est parce que quelques mécanismes d'animation surviennent après viewDidAppear: et l'exécution de performSelector: withObject: afterDelay: reporte l'appel à la boucle de message suivante - dans mon cas cela a fonctionné même avec delay = 0. – Anton