简体   繁体   English

如何设置根视图控制器?

[英]How to set root view controller?

I want to make slide out menu. 我要滑出菜单。 For this I have to create UINavigationViewController which will control MenuViewController (Table View) and ProfileViewControlle r(Content View) 为此,我必须创建UINavigationViewController ,它将控制MenuViewController (表视图)和ProfileViewControlle r(内容视图)

I want to set UINavigationViewController like a rootViewController , for this I wrote this code in AppDelegate.swift 我想将UINavigationViewController设置为rootViewController ,为此我在AppDelegate.swift编写了此代码

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

let nav1 = UINavigationController()
        let mainView = MainViewController(menuViewController: nil, contentViewController: nil) //ViewController = Name of your controller
        nav1.viewControllers = [mainView]
self.window?.rootViewController = nav1

        self.window?.makeKeyAndVisible()

        return true
    }

MainViewController is my UINavigationViewController . MainViewController是我的UINavigationViewController

But here I have error 但是这里我有错误

nil is not compatible with expected argument type 'UIViewController' nil与预期的参数类型'UIViewController'不兼容

What I should do? 我该做什么?

  1. You pass nil here: MainViewController(menuViewController: nil, contentViewController: nil) instead of some view controllers. 您在此处传递nilMainViewController(menuViewController: nil, contentViewController: nil)而不是某些视图控制器。 That's probably the reason it doesn't compile. 这可能是它无法编译的原因。

  2. You embed your MainViewController instance, which is already UINavigationController, into another one UINavigationController. 您将MainViewController实例(已经是UINavigationController)嵌入到另一个UINavigationController中。 That looks wrong too. 看起来也错了。

Here is an example: 这是一个例子:

LoginViewController *loginController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"loginController"]; //or the homeController
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:loginController];
self.window.rootViewController = navController;`

此错误意味着您不能将menuViewController和contentViewController设置为nil ,它们不是可选的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何设置根视图控制器 - How to set root view controller 如何将SwRevealview控制器设置为根视图控制器 - How to Set SwRevealview controller as Root view controller 如何在AppDelegate中将视图控制器设置为根视图控制器 - How to set the view controller as root view controller in AppDelegate 如何有条件地为导航控制器设置根视图控制器 - How to conditionally set root view controller for a Navigation Controller 如何在以编程方式使用导航控制器设置动画的情节提要中设置根视图控制器 - How to set root view controller in storyboard animated with navigation controller programmatically 将根视图控制器设置为模态视图控制器 - Set root view controller to a modal view controller 如何使用过渡将推送视图设置为根视图控制器 - how to set pushed view as root view controller using transition 无法设置根视图控制器 - Cannot set root view controller 在Storyboard中设置View Controller根目录 - Set View Controller root in Storyboard 当前窗口的根视图控制器为UITabBarController时,如何将窗口的根视图控制器设置为UINavingationController? - How to set root view controller of window to a UINavingationController when current root view controller of window is UITabBarController?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM