0

我在 iOS 中有一个项目,我正在尝试修改它以在另一个项目中使用。该项目运行良好,但是当我尝试将侧视图控制器嵌入标签栏控制器时,它给出了一个错误

**MFSideMenuDemoStoryboard[23760:c07] -[UITabBarController setLeftMenuViewController:]: unrecognized selector sent to instance 0x757a590
2013-06-13 10:08:51.062 MFSideMenuDemoStoryboard[23760:c07] **** * * **Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITabBarController setLeftMenuViewController:]: unrecognized selector sent to instance 0x757a590'**

我知道appDelegate.m 中的代码有问题,但无法弄清楚

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];

MFSideMenuContainerViewController *container = (MFSideMenuContainerViewController *)self.window.rootViewController;

UINavigationController *navigationController = [storyboard instantiateViewControllerWithIdentifier:@"navigationController"];


UIViewController *leftSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:@"leftSideMenuViewController"];
UIViewController *rightSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:@"rightSideMenuViewController"];


[container setLeftMenuViewController:leftSideMenuViewController];
[container setRightMenuViewController:rightSideMenuViewController];
[container setCenterViewController:navigationController];

return YES;
}

这是我的故事板

在此处输入图像描述

4

2 回答 2

2

您的根视图控制器不是 MFSideMenuContainerViewController。它是一个 UITabBarController。如果您想使用 MFSideMenuContainerViewController,您需要查看文档:https ://github.com/mikefrederick/MFSideMenu/

这是一个基本示例:

UIViewController *leftSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:@"leftSideMenuViewController"];
UIViewController *rightSideMenuViewController = [storyboard instantiateViewControllerWithIdentifier:@"rightSideMenuViewController"];
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
                                                containerWithCenterViewController: self.window.rootViewController
                                                leftMenuViewController: leftSideMenuViewController
                                                rightMenuViewController: rightSideMenuViewController];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];

这将用 MFSideMenuContainerViewController 替换现有的根视图控制器,并将旧的根视图控制器放置为中心视图控制器。

于 2013-06-13T04:05:27.083 回答
0

正如您在故事板和异常中所见,您的根视图控制器是一个标签栏控制器,而不是一个侧菜单控制器。

UITabBarController 有一个属性viewControllers,可让您访问 tabBarController 内的控制器。您想在索引 0 处获取控制器。这意味着第一个选项卡上的 viewController。

像这样的东西应该工作:

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
MFSideMenuContainerViewController *container = tabBarController.viewControllers[0];
// check that container is actually a container and not something else
NSParameterAssert([container isKindOfClass:[MFSideMenuContainerViewController class]]);
于 2013-06-13T07:30:54.857 回答