在 NavigationController 视图中保留数据/实例变量
我有一个根视图和一个详细视图,在详细视图中是一个带有基本播放/暂停按钮设置的 AVAudioPlayer 。一切正常,但是如果我播放文件然后单击返回 (popViewControllerAnimated) 到根目录,文件将继续播放,这是正确的,但是如果我单击返回详细视图,视图已重置并且找不到 的实例AVAudioPlayer。
Detail 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
Detail 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
。
I have a root view and a detail view, in the detail view is a AVAudioPlayer with a basic play/pause button setup. All works, however if I play the file and then click back (popViewControllerAnimated) to the root, the file keeps playing which is correct, but then if I click back to the detail view the view has reset and doesn't find the instance of the AVAudioPlayer.
Detail 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
Detail 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];
}
}
How do I persist either appSoundPlayer
or playing
when I view the view again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在 RootViewController 上保留对
appSoundPlayer
对象的引用,并在加载时以某种方式将其传递到详细视图。实际上
BOOL Playing
不是必需的,因为它是 AVAudioPlayer 您可以随时访问它。RootViewController.h
RootViewController.m
在你的 DetailPageViewController.h
DetailPageViewController.m
我还没有测试过这段代码,但你可以很好地了解你需要做什么。当然,还有其他方法可以做到这一点。
You would need to keep a reference to the
appSoundPlayer
object on your RootViewController, and pass it in some way to your detail view when loading it.Actually the
BOOL playing
is not necessary, because it is a property of AVAudioPlayer and you can access this whenever you want.RootViewController.h
RootViewController.m
On your DetailPageViewController.h
DetailPageViewController.m
I haven't tested this code but you can have a good idea of what you will need to do. Of course, there are other ways to do this.