草庐IT

ios - "Hide"ViewController 没有完全关闭它

coder 2024-01-22 原文

我正在为 iOS 构建一个播放视频的媒体播放器应用。有一个 TableViewController 允许您选择要播放的视频,然后选择一个打开该视频,在另一个 View Controller 中显示该视频,其下方有自定义媒体控件。我可以关闭该视频,并且出于某种原因,该视频一直在后台播放,但我不确定如何在不创建新实例的情况下将其恢复。我会发布代码,但我什至不确定从哪里开始。

提前致谢。

最佳答案

本质上,您想要创建一个 View Controller 并将其存储在某个地方,该位置在您的应用程序执行(或您不再需要它)之前不会被释放。这可以是您的应用程序委托(delegate)、单例或任何最适合您的东西。这样, View Controller 的单个实例就是您每次想要使用它时所获得的。

下面是我将如何实现它:

  1. 我会在应用程序委托(delegate)上创建一个属性来存储我的 View Controller 以供应用程序的整个执行过程使用。像这样,在您的应用委托(delegate) header 中:

    @property (nonatomic, retain) UIViewController *nowPlayingViewController;

(您可能想让它成为您的实际子类,而不是 UIViewController,因为您可能需要将自定义数据等传递给此 View Controller 实例)

  1. 在您的应用委托(delegate)中为该 View Controller 创建自定义 getter,以便在您首次尝试访问您的 View Controller 时自动创建它。在你的
- (UIViewController *)nowPlayingViewController {
    if (_nowPlayingViewController == nil) {
        // initialize view controller (for a storyboard, you'd do it like so, making sure your storyboard filename and view controller identifier are set properly):
        _nowPlayingViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"NowPlaying"];
    }
    return _nowPlayingViewController;
}

Reminder: if your now playing view controller is embedded in a navigation controller, you're going to want to instantiate that. Remember that if you store your navigation controller in this variable, you'll need to get your now playing view controller from it itself (for example, if you're meaning to call methods or set properties on it). If this is true, maybe store the navigation controller and have a convenience method for returning the now playing view controller (topViewController) itself.

  1. 现在,当您想要显示 View Controller 时,以编程方式进行。如果你想以模态方式呈现它,你可以这样做:
MyAppDelegate *delegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate;
[self presentViewController:delegate.nowPlayingViewController animated:YES completion:nil];
  1. 要关闭,只需像往常一样在 View Controller 上调用 dismissViewControllerAnimated:(如果在导航 Controller 中,则将其弹出,实际上您会这样做)。因为它存储在应用程序 session 期间持续存在的应用程序委托(delegate)中,所以它始终是同一个实例。

  2. 作为奖励,如果您确实想要销毁 View Controller ,可以将委托(delegate)的属性设置为 nil,下次使用它时自动创建。

您的实现可以有所不同,并根据需要进行调整以满足您应用的需求,但这是一个起点。

关于ios - "Hide"ViewController 没有完全关闭它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49267602/

有关ios - "Hide"ViewController 没有完全关闭它的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  4. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  5. ruby - 完全离线安装RVM - 2

    我打算为ruby​​脚本创建一个安装程序,但我希望能够确保机器安装了RVM。有没有一种方法可以完全离线安装RVM并且不引人注目(通过不引人注目,就像创建一个可以做所有事情的脚本而不是要求用户向他们的bash_profile或bashrc添加一些东西)我不是要脚本本身,只是一个关于如何走这条路的快速指针(如果可能的话)。我们还研究了这个很有帮助的问题:RVM-isthereawayforsimpleofflineinstall?但有点误导,因为答案只向我们展示了如何离线在RVM中安装ruby。我们需要能够离线安装RVM本身,并查看脚本https://raw.github.com/wayn

  6. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  7. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  8. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  9. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  10. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

随机推荐