我有一個根視圖和一個詳細視圖,在詳細視圖中是一個帶有基本播放/暫停按鈕設置的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];
}
}
我如何堅持要麼appSoundPlayer
或playing
當我再次查看視圖。
謝謝你的答案。我開始得到它,但得到以下錯誤: '重複成員_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
@daidai,你得到一個警告,因爲你正在尋找的方法不存在,你必須創建方法-initWithParentViewController:initWithNibName:bundle :,基於我創建的方法-initWithParentViewController: – vfn 2010-08-27 05:58:09
謝謝,固定。但它仍然抱怨'重複成員'_parentViewController',但我改變(在DetailPageViewController.h)'RootViewController * _parentViewController;'到'RootViewController * parentViewController;'它編譯,但崩潰(與警告本地聲明'parentViewController'隱藏實例變量和屬性'parentViewController'類型不符合超類'UIViewController'屬性類型。感謝您的幫助,我慢慢理解了原理。 – daidai 2010-08-27 06:25:10