容器V中的父级和子级视图

时间:2013-06-13 04:01:23

标签: ios xcode uiviewcontroller

我在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;
}

这是我的故事板

enter image description here

2 个答案:

答案 0 :(得分:2)

您的根视图控制器不是MFSideMenuContainerViewController。它是一个UITabBarController。如果您想使用MFSideMenuContainerViewController,您需要查看文档:{​​{3}}

这是一个基本的例子:

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替换现有的根视图控制器,并将旧的根视图控制器作为中心视图控制器。

答案 1 :(得分: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]]);