在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獲得大膽快。謝謝。
您是否使用retain或copy屬性聲明瞭ivc(不是一個好的屬性名稱)?如果不是,退出loadView時不會保留,這就是你的問題。 – Felixyz 2010-02-19 14:33:01
是的,我就與reatain財產申報IVC:@property(非原子,保留)InfoViewController * IVC; – Cyprian 2010-02-19 15:02:45