2010-08-27 105 views
0

我有一個根視圖和一個詳細視圖,在詳細視圖中是一個帶有基本播放/暫停按鈕設置的AVAudioPlayer。所有的作品,但是如果我播放文件,然後單擊回(popViewControllerAnimated)到根目錄,該文件繼續播放哪個是正確的,但如果我點擊回到詳細視圖的視圖已重置,並沒有找到AVAudioPlayer。在NavigationController中查看數據/實例變量查看

詳細page.h

@interface DetailPageViewController : UIViewController <AVAudioPlayerDelegate>{ 
UIButton *arrowBtn; 
UIButton *playButton; 
UIButton *pauseButton; 
AVAudioPlayer *appSoundPlayer; 
NSURL *soundFileURL; 
BOOL playing ; 
UILabel *timeLabel; 
NSTimer *timer; 
} 

@property (nonatomic, retain) IBOutlet UIButton *arrowBtn; 
@property (nonatomic, retain) IBOutlet UIButton *playButton; 
@property (nonatomic, retain) IBOutlet UIButton *pauseButton; 
@property (nonatomic, retain) AVAudioPlayer *appSoundPlayer; 
@property (nonatomic, retain) NSURL *soundFileURL; 
@property (nonatomic, retain) IBOutlet UILabel *timeLabel; 
@property (readwrite) BOOL playing; 
@property (nonatomic, retain) NSTimer *timer; 

-(IBAction)playPauseToggle:(id)sender; 
-(IBAction)returnToRoot; 

@end 

詳細page.m

@synthesize arrowBtn, playButton, pauseButton, playing, appSoundPlayer, soundFileURL, timeLabel, timer; 

... 

-(IBAction)playPauseToggle:(id)sender 
{ 
if (playing == NO) 
{ 
    [appSoundPlayer play]; 
    playing = YES; 
    [playButton setHidden: YES]; 
    [pauseButton setHidden: NO]; 

} 
else { 
    [appSoundPlayer pause]; 
    playing = NO; 
    [playButton setHidden: NO]; 
    [pauseButton setHidden: YES]; 
} 
} 

-(IBAction)returnToRoot 
{ 
[self.navigationController popViewControllerAnimated:YES]; 
} 

- (void)viewDidLoad { 
[super viewDidLoad]; 

if (self.appSoundPlayer == nil) 
{ 
    playing = NO; 

    [playButton setHidden: NO]; 
    [pauseButton setHidden: YES]; 

    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"soundFile" ofType:@"mp3"]; 

    NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath]; 
    self.soundFileURL = newURL; 
    [newURL release]; 

    AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: nil]; 
    self.appSoundPlayer = newPlayer; 
    [newPlayer release]; 

    [appSoundPlayer prepareToPlay]; 
    [appSoundPlayer setVolume: 1.0]; 
    [appSoundPlayer setDelegate: self]; 
} 

}

我如何堅持要麼appSoundPlayerplaying當我再次查看視圖。

回答

2

您需要在RootViewController上保留對appSoundPlayer對象的引用,並在加載它時以某種方式將其傳遞給您的詳細視圖。

其實BOOL playing沒有必要,因爲它是AVAudioPlayer的財產,你可以隨時訪問它。

RootViewController.h

@interface RootViewController : UIViewController { 
    AVAudioPlayer *_appSoundPlayer; 

// Declare the other iVars 
} 

@property (nonatomic, retain) AVAudioPlayer *appSoundPlayer; 

@end 

RootViewController.m

@synthesize appSoundPlayer = _appSoundPlayer; 

- (void)dealloc { 
    [self setAppSoundPlayer:nil]; 
    // Dealloc the other properties that you may have 

    [super dealloc]; 
} 
/* 
** Your code 
*/ 

// When presenting the detail view 
- (void)openDetailView { 
    DetailPageViewController *viewController = [[DetailPageViewController alloc] initWithParentViewController:self]; 
. 
. 
. 
    // Present the view controller 
} 

在您DetailPageViewController.h

@interface DetailPageViewController : UIViewController <AVAudioPlayerDelegate> { 
RootViewController *_parentViewController; 
    // Declare the iVars 
} 

@property (nonatomic, assign) RootViewController *parentViewController; 

- (id)initWithParentViewController:(RootViewController *)parentViewController; 

@end 

DetailPageViewController.m

@synthesize parentViewController = _parentViewController; 

- (void)dealloc { 
    // Set the delegate to nil 
    [self.parentViewController.appSoundPlayer setDelegate:nil]; 

    // Dealloc the other properties that you may have 

    [super dealloc]; 
} 

- (id)initWithParentViewController:(RootViewController *)parentViewController { 
    if (self = [super init]) { 
     self.parentViewController = parentViewController; 
    } 
    return self; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    if (self.parentViewController.appSoundPlayer == nil) { 
     [playButton setHidden: NO]; 
     [pauseButton setHidden: YES]; 

     NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"soundFile" ofType:@"mp3"]; 

     NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath]; 
     self.soundFileURL = newURL; 
     [newURL release]; 

     AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: nil]; 
     [newPlayer prepareToPlay]; 
     [newPlayer setVolume: 1.0]; 
     [newPlayer setDelegate: self]; 
     self.parentViewController.appSoundPlayer = newPlayer; 
     [newPlayer release]; 
    } 
} 

我還沒有測試過這個代碼,但是你可以對你需要做什麼有一個很好的想法。當然,還有其他方法可以做到這一點。

+0

謝謝你的答案。我開始得到它,但得到以下錯誤: '重複成員_parentViewController','警告:沒有-initWithParentViewController:initWithNibName:捆綁:'找到方法' & '不兼容的Objective-C類型返回'結構UIViewController *',預計'結構RootViewController *' 是否有關係,我使用的是DetailPage的initWithNibName?像'DetailPageViewController * detailPage = [[DetailPageViewController alloc] initWithParentViewController:self initWithNibName:@「DetailPage」bundle:nil];'? – daidai 2010-08-27 05:33:35

+0

@daidai,你得到一個警告,因爲你正在尋找的方法不存在,你必須創建方法-initWithParentViewController:initWithNibName:bundle :,基於我創建的方法-initWithParentViewController: – vfn 2010-08-27 05:58:09

+0

謝謝,固定。但它仍然抱怨'重複成員'_parentViewController',但我改變(在DetailPageViewController.h)'RootViewController * _parentViewController;'到'RootViewController * parentViewController;'它編譯,但崩潰(與警告本地聲明'parentViewController'隱藏實例變量和屬性'parentViewController'類型不符合超類'UIViewController'屬性類型。感謝您的幫助,我慢慢理解了原理。 – daidai 2010-08-27 06:25:10