如何从堆栈弹出视图控制器时保留它?
基本上我有一个Mp3播放器视图控制器。当我在那个视图控制器中时,我可以播放音乐。当音乐正在播放并且我导航到另一个控制器时,音乐停止播放,当我导航回该控制器时,它启动另一个实例。所以我猜测修复,可能会保留视图控制器吗?
这就是我启动音乐播放器视图控制器的方式:
NSString *songLargeArtworkURL = [self.songArtworkURL stringByReplacingOccurrencesOfString:@"large" withString:@"crop"];
MusicPlayerViewController *musicPlayer = [[MusicPlayerViewController alloc] init];
musicPlayer.songName = self.testString;
musicPlayer.songArtWork = songLargeArtworkURL;
musicPlayer.songStream = self.songStreamURL;
[self.navigationController pushViewController:musicPlayer animated:YES];
答案 0 :(得分:1)
不使用MusicPlayerViewController
的局部变量,而是使用实例变量。仅在第一次时,分配musicPlayer
实例。如下所示:
NSString *songLargeArtworkURL = [self.songArtworkURL stringByReplacingOccurrencesOfString:@"large" withString:@"crop"];
if(! musicPlayer) {
musicPlayer = [[MusicPlayerViewController alloc] init];
}
musicPlayer.songName = self.testString;
musicPlayer.songArtWork = songLargeArtworkURL;
musicPlayer.songStream = self.songStreamURL;
[self.navigationController pushViewController:musicPlayer animated:YES];
上述代码确保仅在第一次实例化音乐播放器时。随后的时间,它会推动音乐播放器的同一个实例