在 NavigationController 视图中保留数据/实例变量

发布于 2024-09-16 05:01:08 字数 2294 浏览 6 评论 0原文

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

}

当我再次查看视图时,如何保持 appSoundPlayerplaying

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜柠檬 2024-09-23 05:01:08

您需要在 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];
    }
}

我还没有测试过这段代码,但你可以很好地了解你需要做什么。当然,还有其他方法可以做到这一点。

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

@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
}

On your 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];
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文