2

在以下 ViewControllerClass 中,当尝试在 ViewDidAppear 方法中调用 presentModalViewController 时,我得到 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

方法调用

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

在 ViewDidAppear 中导致 EXC_BAD_ACCESS。

我曾尝试使用 NSZombieEnabled 对其进行调试,但只获得了对 main 的堆栈调用。让我抓狂的是,如果从方法playbackStateChanged 运行相同的代码,它就可以正常工作。

如果你们中的任何一个可以帮助我,我就不会那么快。谢谢。

4

1 回答 1

2

我终于让它工作了!但我认为这只是快速修复。

所以我发现要让我的 ivc 出现,我需要延迟对 presentModalViewController 的调用

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

就是这样。有用。

我不知道为什么这会有所帮助,所以如果你们中的一位大师对此了解更多,请赐教。

于 2010-02-19T21:32:49.530 回答