0

So I have an app that looks like this: enter image description here

When I click on the radio icon, it loads the radio view using:

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

[self presentModalViewController:vc animated:YES];

enter image description here

but when I dismiss it (like when I am listening to the radio), I want it to keep playing, which it does, but the thing is, when I click it again, it loads a new instance of that view, wasting precious memory. enter image description here

I am wondering how I would get that first instance of the radio.

Any suggestions?

Piyush Dubey
  • 2,416
  • 1
  • 24
  • 39
x86cam
  • 407
  • 1
  • 5
  • 13
  • 1
    Maybe you could use some smaller image thumbnails and link to the full version, in your question? It's hard to read when the images is taller than the browser window. – Stein G. Strindhaug Jan 17 '14 at 14:35

3 Answers3

1

You can add the instance variable for the view controller to your AppDelegate. Instead of instantiating it unconditionally, you can check whether it is nil. In the instantiating view controller:

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];
user3071962
  • 320
  • 3
  • 8
  • So I put it in, along with the declaration code in the app delegate: `@property (strong, nonatomic) BDNRadioViewController *radioViewController;`, It works when I click it the first time. It crashes when I close and open it the next time. – x86cam Jan 19 '14 at 03:24
  • The error was `Application tried to present modally an active controller ` – x86cam Jan 19 '14 at 03:36
  • It seems that the radioViewController is still active. Other posts suggest that this can be due to ongoing animations. Have you seen this question: [Application tried to present modally an active controller](http://stackoverflow.com/q/7429014/3071962) – user3071962 Jan 19 '14 at 10:10
0

You can save the radio state on the presenting view controller and only show UI state in the presented view controller.

So you can implement your presented view controller as below:

    -(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
    ...
Danyun Liu
  • 3,072
  • 24
  • 22
0

Rather than using different view controllers, you can use a single viewcontroller with different views in it which you can present and hide according to desired conditions and also by this you have to initialize this only once.

Also if you need a block to be called only one time you can use below code:

dispatch_once(&once, ^ { // Your Code here });

Ashutosh
  • 2,215
  • 14
  • 27