0

I implemented SWRevealViewController in my project for Side menu item. Basically am app is kind of Music application. Songs from Home screen will remain playing continuously if the user in some other screen or in background. Am following this tutorial AppCoda (http://www.appcoda.com/ios-programming-sidebar-navigation-menu/)

  1. When the app launching the Home Screen will be launched also start to play song.
  2. If the user goes to another screen like playlist from the side menu item. The Home screen is in Stack and the song is playing perfectly. The Playlists screen is in Front.
  3. Again I go the Home screen from Side menu item. The new instance is creating instead of going to the already created Home screen. Now, am able to listen two songs at a time. One from first Home Screen and another one from new Home Screen.

This is happening for all screens. How can I solve this issue? I want only one screen from the stack instead of creating the same screen in many times.

Here is my Code from Side menu tableview Controller,

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 1) {
        UINavigationController *navController;

        if (indexPath.row == 0) {

            ViewController *homeVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
            navController = [[UINavigationController alloc] initWithRootViewController:homeVC];
            [navController setViewControllers: @[homeVC] animated: YES];

        } else if (indexPath.row == 1) {

            SongsListViewController *songsListVC = [self.storyboard instantiateViewControllerWithIdentifier:@"SongsListViewController"];
            navController = [[UINavigationController alloc] initWithRootViewController:songsListVC];
            [navController setViewControllers: @[songsListVC] animated: YES];

        } else if (indexPath.row == 2) {
            PlayListViewController *songsListVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PlayListViewController"];
            navController = [[UINavigationController alloc] initWithRootViewController:songsListVC];
            [navController setViewControllers: @[songsListVC] animated: YES];
        }

        [self.revealViewController setFrontViewController:navController];
        [self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
    }
}

Looking forward your help. Thanks in advance.

Yuvaraj.M
  • 9,741
  • 16
  • 71
  • 100
  • refer this link http://www.appcoda.com/ios-programming-sidebar-navigation-menu/ – Bandish Dave Nov 25 '15 at 06:27
  • Thank you. I followed this tutorial only. Actually whenever we are navigating to some other screen from Side Menu TableView new instance is creating instead of using the previous one. So the Same viewcontrollers are adding in Stack many times. Any idea? Am not able to fine the issue where am going wrong. – Yuvaraj.M Nov 25 '15 at 06:36
  • Yuvaraj please see the Meharoof Najeeb answer. – user3182143 Nov 25 '15 at 07:05
  • Use a Singleton class for the player so that there is only one player at all times. – ShahiM Nov 25 '15 at 10:18

3 Answers3

1

In fact you need change the playing handling to singleton, not the viewcontroller

in SWSongPlayManagerSingleton.h

#import <Foundation/Foundation.h>

@interface SWSongPlayManagerSingleton : NSObject
+ (instancetype)sharedInstance;
@end

in SWSongPlayManagerSingleton.m

@implementation SWSongPlayManagerSingleton 
+ (instancetype)sharedInstance {
   static SWSongPlayManagerSingleton *singleton = nil;
   static dispatch_once_t token;
   dispatch_once(&token, ^{
       singleton = [[self alloc] init];
   });

   return singleton;
}
@end

now you access the SWSongPlayManagerSingleton instance by [SWSongPlayManagerSingleton sharedInstance] which always gives you the same instance.

SeanChense
  • 846
  • 8
  • 14
  • Thank you @SeanChense. But, it is happening for all Viewcontrollers. For example, in Side menu I have 3 options (3View controllers). First time 1st Viewcontroller created in Front View and song is playing. Going to the side menu and clicking 2nd option and going to 2nd VC. If again am going to 1st VC new instance is creating instead of using the older from the stack. It is happening for all VC's like 2nd and 3rd. Any idea? Thank you. – Yuvaraj.M Nov 25 '15 at 06:40
  • I will try your solution. Thank you. – Yuvaraj.M Nov 25 '15 at 06:40
  • In fact you need change the playing handling to singleton, not the viewcontroller. – SeanChense Nov 25 '15 at 06:43
1

Actually, you already have all the view controllers in the stack. You don't have to create new view controllers, as you are doing with "instantiateViewControllerWithIdentifier" method.

You have to reference the navigation controller object and you can access the array of view controllers in the stack with viewControllers property of the navigation controller object.

UINavigationController *navController = self.navigationController;

This should get the reference to your navigation controller.

NSArray *viewControllers = navController.viewControllers;

This should get you the viewControllers already present in the stack. You can then loop through this viewControllers' array to check for an instance of your HomeViewController using

for (UIViewController *obj in viewControllers)
{
    if([obj isKindOfClass:[HomeViewController class]])
    {
        // Your code
    }
}
Skywalker
  • 1,590
  • 1
  • 18
  • 36
  • Thank you Meharoof. Hope I need to check this in Side Menu TableView controller DidSelectRowAtIndexPath delegate. I embedded this VC in NavigationController and tried to take the stack of VCs. Here we are not pushing VC from Side menu VC. So, the stack always showing the currenct VC (SideMenuViewController). Instead of that we are setting the HomeVC, SongsListVC, other VC's as FontView of SWRevealViewController. Also I tried to take the SWRevealVC's navigationcontroller stack, it is returning nil. Am trying. If you have any solution pls help me. Thanks. – Yuvaraj.M Nov 25 '15 at 07:21
1

Fact that you can listen multiple songs for some cases, I think your player is not deallocated properly when it is supposed to. Also if you want to play/control the player regardless of where you are in the app, you should find a global place or singleton to handle that. Maybe in the Side menu table view controller??

I'm not familiar with SWRevealViewController but what I can say is that you are creating a new instance every time you select a menu in the table view. If you don't want that, you should have homeVC, songsListVC, and songsListVC as properties of Side menu table view controller and should do lazy load. i.e) You only create them if it is nil.

HMHero
  • 2,333
  • 19
  • 11