简体   繁体   English

iOS将视图保存在内存中

[英]ios keep view in memory

I'm creating an app that plays music and has a nice UI for the album art and progress bar. 我正在创建一个播放音乐的应用程序,并具有用于专辑封面和进度条的漂亮UI。 The problem is when the user leaves the player view and goes back, the player UI has to be reloaded and you see a flash while the progress bar is resized and the album art is loaded. 问题是,当用户离开播放器视图并返回时,必须重新加载播放器UI,并且在调整进度栏的大小和加载专辑封面时会看到闪烁。

Is there any way to keep items in memory when you leave a controller? 离开控制器后,有什么方法可以将项目保留在内存中吗? Similar to how you go between playlists and player view on the iPhone music app, there is no lag or delay in seeing the artwork or song progress, it's there from the beginning. 与您在iPhone音乐应用程序上的播放列表和播放器视图之间切换的方式类似,从一开始就没有延迟或延迟看到插图或歌曲的进度。

Or am I thinking about this the wrong way? 还是我在想这是错误的方式?

I am assuming you are going back and forth using a UINavigationController , if so the default behavior is that the view controller will be released when you go back, as the only strong reference to it is in the UINavigationController stack, when it is popped the reference is lost, and therefore your controller is deallocated. 我假设您正在使用UINavigationController来回移动,如果是这样,则默认行为是视图控制器将在您返回时释放,因为对其唯一的强引用是在UINavigationController堆栈中,当弹出该引用时丢失,因此控制器被释放。

If you want to avoid this, all you need to do is have any other object have a strong reference to your view controller. 如果要避免这种情况,您需要做的就是让其他任何对象对您的视图控制器都具有强引用。 An easy way to do it would be whenever you initialize your controller, in that class have a strong property that holds a reference to the view controller you do not want to lose. 一种简单的方法是在您初始化控制器时,该类中的类具有强大的属性,该属性包含对您不想丢失的视图控制器的引用。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM