代码之家  ›  专栏  ›  技术社区  ›  williej926

自己revealViewController()返回nil

  •  1
  • williej926  · 技术社区  · 7 年前

    https://github.com/John-Lluch/SWRevealViewController

        import Foundation
        import UIKit
        class MainViewController : UIViewController{
        @IBOutlet weak var mainButton: UIView!
        @IBOutlet weak var Open: UIBarButtonItem!
        override func viewDidLoad() {
            super.viewDidLoad()
    
        }
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated);
            mainButton.makeCircle();
            Open.target = self.revealViewController()
            Open.action = #selector(SWRevealViewController.revealToggle(_:));
            self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer());
        }
    }
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   Lawliet    7 年前

    很可能您还没有设置 SWRevealViewController rootViewController .记住设置 自定义类 成为 在你的 身份检查员 .

    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 .

    if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
        appDelegate.changeRootVCToSWRevealVC()
    }
    

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

    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...
    }
    
        2
  •  0
  •   Chris Sprague    6 年前

    我也遇到了同样的问题,于是我看到了这篇文章。我的问题更像是“哎呀!”我错过了一些显而易见的事情,花了30分钟挠头脸掌:

    如果接受的答案不适合您,请检查以确保您已使用插座将菜单属性连接到故事板。

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