views:

400

answers:

1

In the Follwoing ViewControllerClass I get EXC_BAD_ACCESS when trying to call presentModalViewController in ViewDidAppear method.

#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

Method call

 [self presentModalViewController:self.ivc animated:YES];

in ViewDidAppear causes EXC_BAD_ACCESS.

I have tried debuging it with NSZombieEnabled but got only a stack call to main. The thing that gets me crazy is that if the same code is run from method playbackStateChanged it works fine.

If any of you can help I wan't get bold as quick. Thanks.

+1  A: 

I finally made it to work! But I think it's just quick fix.

So I found out that to make my ivc to show up I need to delay the call to presentModalViewController

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

That's it. It works.

I don't know why this helped, so if one of you gurus knows more about it please enlighten me.

Cyprian