Swift Self.revealViewController()返回nil

Swift Self.revealViewController()返回nil,swift,swrevealviewcontroller,Swift,Swrevealviewcontroller,我目前正试图让平移手势显示控制器。现在,我的“打开”按钮起作用,但是当我添加最后一行时,我的程序崩溃了,因为revealViewController为零。连接到pod: 您可能没有将SWRevealViewController设置为rootViewController。记得在身份检查器中将自定义类和情节提要ID设置为SWRevealViewController func application(_ application: UIApplication, didFinishLaunchingWit

我目前正试图让平移手势显示控制器。现在,我的“打开”按钮起作用,但是当我添加最后一行时,我的程序崩溃了,因为revealViewController为零。连接到pod:


您可能没有将
SWRevealViewController
设置为
rootViewController
。记得在身份检查器中将自定义类和情节提要ID设置为
SWRevealViewController

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let rootVC = storyboard.instantiateViewController(withIdentifier: "SWRevealViewController")    
    self.view.window?.rootViewController = rootVC

    // Cont. with your code...
}

如果需要在开始时显示登录页面,则需要在
AppDelegate
中提供一个函数,该函数允许窗口更新
rootViewController

func changeRootVCToSWRevealVC () {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let rootVC = storyboard.instantiateViewController(withIdentifier: "SWRevealViewController")
    if let window = self.window{
        window.rootViewController = rootVC
    }
}
用户登录后,可以将
rooViewController
更改为
SWRevealViewController

if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
    appDelegate.changeRootVCToSWRevealVC()
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    let rootVCId = isLoggedin ? "SWRevealViewController" : "LoginViewController"
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let rootVC = storyboard.instantiateViewController(withIdentifier: rootVCId)    
    self.view.window?.rootViewController = rootVC

    // Cont. with your code...
}
如果您有一个存储上次登录状态的变量,并且希望直接导航到导航抽屉菜单,而不是
LoginViewController

if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
    appDelegate.changeRootVCToSWRevealVC()
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    let rootVCId = isLoggedin ? "SWRevealViewController" : "LoginViewController"
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let rootVC = storyboard.instantiateViewController(withIdentifier: rootVCId)    
    self.view.window?.rootViewController = rootVC

    // Cont. with your code...
}

我也遇到了同样的问题,所以看到了这篇文章。我的问题更像是一个“哎呀!”的问题,因为我错过了一些明显的东西,花了30分钟时间挠头facepalm:

如果接受的答案不适用于您,请检查以确保您已使用插座将菜单属性连接到情节提要

斯威夫特:

@IBOutlet weak var menuButton:UIBarButtonItem!

override func viewDidLoad() {
    if (self.revealViewController() != nil) {
        menuButton.target = self.revealViewController()
        menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
    }
}

对不起,如果这听起来很愚蠢,但我应该把它放在哪里?我假设它是应用程序代理,但我已经有另一个根视图控制器用于用户登录;从而让应用程序崩溃。是有修复程序还是我放错地方了?谢谢你花时间来帮助我!我更新了我的答案。如果您有一个存储上次登录状态的变量,并且希望直接导航到导航抽屉菜单,而不是强制用户重新登录,您可以修改
didfishlaunchingwithoptions
,以检查
rootViewController
是否应为
LoginViewController
swreveviewcontroller
@Lawliet我已尝试过您的代码,但它对我不起作用我也仅使用swreviewcontroller