gpt4 book ai didi

ios - 弹出 Objective -C 时如何保留 ViewController

转载 作者:行者123 更新时间:2023-11-29 02:51:38 25 4
gpt4 key购买 nike

当 View Controller 从堆栈中弹出时,如何保留它?

基本上我有一个 Mp3 播放器 View Controller 。当我在那个 View Controller 中时,我可以播放音乐。当音乐正在播放并且我导航到另一个 Controller 时,音乐停止播放,当我导航回该 Controller 时,它会启动它的另一个实例。所以我想解决办法可能是保留 View Controller ?

这就是我启动音乐播放器 View Controller 的方式:

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];

最佳答案

不要使用 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];

上面的代码确保,仅在第一次实例化音乐播放器。随后,它会推送音乐播放器的同一实例

关于ios - 弹出 Objective -C 时如何保留 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24422397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com