2010-02-19 116 views
2

在Follwoing ViewControllerClass我在嘗試調用presentModalViewController在ViewDidAppear方法時EXC_BAD_ACCESS。加載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調試運行,但只拿到了一組調用主。 讓我瘋狂的事情是,如果從方法playbackStateChanged運行相同的代碼,它工作正常。

如果你們能幫助我wan't獲得大膽快。謝謝。

+0

您是否使用retain或copy屬性聲明瞭ivc(不是一個好的屬性名稱)?如果不是,退出loadView時不會保留,這就是你的問題。 – Felixyz 2010-02-19 14:33:01

+0

是的,我就與reatain財產申報IVC:@property(非原子,保留)InfoViewController * IVC; – Cyprian 2010-02-19 15:02:45

回答

2

我終於做到了工作!但我認爲這只是快速修復。

於是我發現,使我IVC展現出來,我需要調用耽誤presentModalViewController

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

就是這樣。有用。

我不知道爲什麼會幫助,所以如果你大師一個人知道更多關於它請賜教。

+0

我有同樣的問題,它在我的iPhone 2G iOS 3.1.2上崩潰,並且不會在模擬器中崩潰。我猜這個解決方案工作的原因是因爲一些動畫機制仍然發生在viewDidAppear之後:並執行performSelector:withObject:afterDelay:推遲到下一個消息循環的調用 - 在我的情況下,即使延遲= 0也可以。 – Anton 2010-11-17 13:56:19