我有一个根视图和一个详细视图,在详细视图中是一个带有基本播放/暂停按钮设置的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