gpt4 book ai didi

ios - SWRevealViewController 设置前端 VC Objective-C

转载 作者:行者123 更新时间:2023-11-28 19:42:18 24 4
gpt4 key购买 nike

我在侧边菜单项的项目中实现了 SWRevealViewController。基本上我的应用程序是一种音乐应用程序。如果用户在其他屏幕或背景中,主屏幕中的歌曲将继续播放。我正在学习本教程 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.

所有屏幕都会发生这种情况。我该如何解决这个问题?我只想要堆栈中的一个屏幕,而不是多次创建相同的屏幕。

这是我在侧边菜单 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];
}
}

期待您的帮助。提前致谢。

最佳答案

实际上你需要将播放处理更改为单例,而不是 View Controller

SWSongPlayManagerSingleton.h

#import <Foundation/Foundation.h>

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

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

现在您可以通过以下方式访问 SWSongPlayManagerSingleton 实例[SWSongPlayManagerSingleton sharedInstance] 始终为您提供相同的实例。

关于ios - SWRevealViewController 设置前端 VC Objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33909661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com