Reputation: 9811
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/)
- When the app launching the Home Screen will be launched also start to play song.
- 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.
- 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.
Upvotes: 0
Views: 472
Reputation: 1586
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
}
}
Upvotes: 1
Reputation: 2343
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.
Upvotes: 1
Reputation: 846
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.
Upvotes: 1