JustMe
JustMe

Reputation: 316

"Hide" ViewController without fully dismissing it

I'm building a media player app for iOS that plays videos. There is a TableViewController that allows you to select a video to play, and selecting one opens that video, showing the video in another View Controller, with custom media controls below it. I'm able to dismiss the video, and for some reason, the video keeps playing in the background, but I'm not sure how to bring it back up without creating a new instance of it. I would post code, but I'm not even sure where to start on this one.

Thanks in advance.

Upvotes: 2

Views: 1545

Answers (1)

AppleBetas
AppleBetas

Reputation: 88

In essence, you want to create a view controller and store it somewhere that won't get deallocated until your app does (or you no longer need it). This could be your application delegate, a singleton, or whatever works best for you. This way, that single instance of the view controller is what you get each time you want to use it.

Here's how I'd implement it:

  1. I'd create a property on the app delegate to store my view controller for the entirety of my application's execution. Like so, in your app delegate header:

    @property (nonatomic, retain) UIViewController *nowPlayingViewController;

(you'll probably want to make it your actual subclass, rather than UIViewController, as you'll likely need to pass your custom data, etc, to this view controller instance)

  1. Create a custom getter for that view controller in your app delegate, so that it is automatically created when you first try to access your view controller. In your
- (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. Now, when you want to show your view controller, do it programatically. If you want to present it modally, you could do it like so:
MyAppDelegate *delegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate;
[self presentViewController:delegate.nowPlayingViewController animated:YES completion:nil];
  1. To dismiss, just call dismissViewControllerAnimated: on the view controller like normal (or pop it if in a navigation controller, really however you'd do it). Because it's stored in your app delegate which persists for your application's session, it will always be the same instance.

  2. As a bonus, if you ever do want to destroy your view controller, you can set the property on the delegate to nil, and the next time you use it it'll be automatically created.

Your implementation can differ and adapt to suit your app's needs as necessary, but this is a starting point.

Upvotes: 3

Related Questions