【问题标题】:Restoring to an already open view using segues使用 segues 恢复到已经打开的视图
【发布时间】:2014-02-06 12:12:53
【问题描述】:

所以我有一个看起来像这样的应用程序:

当我点击收音机图标时,它会使用以下方式加载收音机视图:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"RadioView"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];

[self presentModalViewController:vc animated:YES];

但是当我关闭它时(比如我在听收音机),我希望它继续播放,它确实如此,但问题是,当我再次点击它时,它会加载该视图的一个新实例,浪费宝贵的记忆。

我想知道如何获得收音机的第一个实例。

有什么建议吗?

【问题讨论】:

  • 也许您可以在您的问题中使用一些较小的图像缩略图并链接到完整版本?当图像高于浏览器窗口时,很难阅读。

标签: ios objective-c segue


【解决方案1】:

您可以将视图控制器的实例变量添加到您的 AppDelegate。您可以检查它是否为 nil,而不是无条件地实例化它。 在实例化视图控制器中:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

if (appDelegate.radioViewController == nil) {
   //instantiate the radio view controller
   UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
   appDelegate.radioViewController = [storyboard instantiateViewControllerWithIdentifier:@"RadioView"];
   [appDelegate.radioViewController setModalPresentationStyle:UIModalPresentationFullScreen];
}

// present the radio view controller
[self presentModalViewController:appDelegate.radioViewController animated:YES];

【讨论】:

  • 所以我把它和应用程序委托中的声明代码一起放入:@property (strong, nonatomic) BDNRadioViewController *radioViewController;,当我第一次点击它时它就可以工作。当我下次关闭并打开它时它会崩溃。
  • 错误是Application tried to present modally an active controller <BDNMenuViewController: 0xb17db60>
  • 看来 radioViewController 仍然处于活动状态。其他帖子表明这可能是由于正在进行的动画。你见过这个问题吗:Application tried to present modally an active controller
【解决方案2】:

您可以将单选状态保存在呈现的视图控制器上,并且只在呈现的视图控制器中显示 UI 状态。

所以你可以实现如下所示的视图控制器:

    -(void)viewDidLoad
   {
     [self.presentingViewController start]; //start or continue the pausing radio.
   }

    - (void)start
    {
      //the actually start control action is in your presenting view controller;
      [self. presentingViewController start];
    }
    //update the playing UI
    ...

【讨论】:

    【解决方案3】:

    您可以使用具有不同视图的单个视图控制器,而不是使用不同的视图控制器,您可以根据需要的条件显示和隐藏这些视图,而且您只需初始化一次。

    另外,如果你需要一个块只被调用一次,你可以使用下面的代码:

    dispatch_once(&once, ^ { // 你的代码在这里 });

    【讨论】:

      猜你喜欢
      • 2020-01-14
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-13
      • 1970-01-01
      相关资源
      最近更新 更多