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

时间:2010-08-27 03:16:29

标签: iphone objective-c uinavigationcontroller avaudioplayer

我有一个根视图和一个详细视图,在详细视图中是一个带有基本播放/暂停按钮设置的AVAudioPlayer。所有工作,但是如果我播放文件然后单击返回(popViewControllerAnimated)到根,文件继续播放是正确的,但是如果我单击回到详细视图,视图已重置并且找不到实例AVAudioPlayer。

详情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

1 个答案:

答案 0 :(得分: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];
    }
}

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