Загрузка presentModalViewController в ViewDidAppear вызывает EXC_BAD_ACCESS - PullRequest
2 голосов
/ 19 февраля 2010

В Follwoing ViewControllerClass я получаю EXC_BAD_ACCESS при попытке вызвать presentModalViewController в методе ViewDidAppear.

#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

вызов метода

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

в ViewDidAppear вызывает EXC_BAD_ACCESS.

Я попытался отладить его с помощью NSZombieEnabled, но получил только стековый вызов main. То, что сводит меня с ума, так это то, что если тот же код запускается из метода PlayStateChanged, он работает нормально.

Если кто-нибудь из вас сможет помочь, я не стану так смелым. Благодарю.

1 Ответ

2 голосов
/ 20 февраля 2010

Я наконец сделал это, чтобы работать! Но я думаю, что это просто быстрое решение.

Итак, я обнаружил, что для того, чтобы мой IVC появился, мне нужно отложить вызов presentModalViewController

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

Вот и все. Оно работает.

Я не знаю, почему это помогло, поэтому, если один из вас, гуру, знает об этом больше, пожалуйста, просветите меня.

...