Objective c 在ViewDidDisplay中加载presentModalViewController会导致EXC\u访问错误

Objective c 在ViewDidDisplay中加载presentModalViewController会导致EXC\u访问错误,objective-c,exc-bad-access,Objective C,Exc Bad Access,在下面的ViewControllerClass中,当尝试在ViewDidAspect方法中调用presentModalViewController时,我获得了EXC_BAD_访问权限 #import "SounderViewController.h" #import "ASIFormDataRequest.h" #import "ASIHTTPRequest.h" #import "JSON.h" #import "InfoViewController.h" @implementation

在下面的ViewControllerClass中,当尝试在ViewDidAspect方法中调用presentModalViewController时,我获得了EXC_BAD_访问权限

#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];
在视图中,显示会导致EXC\u访问错误

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


如果你们中有人能帮忙的话,我可不想这么快就冒失了。谢谢

我终于成功了!但我认为这只是权宜之计

所以我发现,要让我的ivc显示出来,我需要延迟呼叫presentModalViewController

[self performSelector:@selector(showWaitingMessageView:) withObject:self.ivc afterDelay:1]; 
就这样。它起作用了


我不知道这有什么帮助,所以如果你们中的一位大师知道更多,请告诉我。

您是否使用retain或copy属性声明了ivc(不是一个好的属性名)?如果没有,则在退出loadView时不会保留它,这将是您的问题。是的,我确实使用reatain属性声明了ivc:@property(nonatomic,retain)InfoViewController*ivc;我也有同样的问题,它在我的iPhone 2G iOS 3.1.2上崩溃,在模拟器上也不会崩溃。我想这个解决方案之所以有效,是因为在viewdide:和performSelector:withObject:afterDelay:延迟调用下一个消息循环的执行之后仍然会出现一些动画机制-在我的例子中,它甚至在delay=0的情况下也能工作。