在Swift 3中滑出菜单

时间:2017-07-27 17:33:08

标签: ios swift uitableview

我一直在关注本教程Youtube,我正在尝试制作一个幻灯片菜单,但是在解开可选值时我意外地发现了一个错误。我相信它与self.storyboard有什么关系?.instantiateViewController(withIdentifier:" SlideMenuView")as! SlideMenuView?但我不确定,我的代码如下:

class CategoryTableView: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate  {



        @IBOutlet weak var menuTableView: ExpandableTableView!
        var slideMenuVC: SlideMenuView?
        var ref: DatabaseReference!
        var categories = [Category]()

        override func viewDidLoad() {
            super.viewDidLoad()
            menuTableView.allHeadersInitiallyCollapsed = true
            menuTableView.initiallyExpandedSection = 1
            menuTableView.singleSelectionEnable = true
            slideMenuVC = self.storyboard?.instantiateViewController(withIdentifier: "SlideMenuView") as! SlideMenuView?
            categoryTableViewCleanUp()
            ref = Database.database().reference()
            loadData()
           // registerForPushNotifications()
        }
        // Menu Table
        func categoryTableViewCleanUp() {
            menuTableView?.delegate = self
            menuTableView?.dataSource = self
        }
        // MARK: Notifications
        /*func getNotificationSettings() {
            UNUserNotificationCenter.current().getNotificationSettings { (settings) in
                print("Notification settings: \(settings)")
                guard settings.authorizationStatus == .authorized else { return }
                UIApplication.shared.registerForRemoteNotifications()
            }
        }
       func registerForPushNotifications() {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
                (granted, error) in
                print("Permission granted: \(granted)")
            }
            guard granted else { return }
            self.getNotificationSettings()
        } */
        // MARK: Slideout Menu
        @IBAction func menuAction(_ sender: UIBarButtonItem) {
            // Show menu
            if AppDelegate.slideMenuBool{
                showSlideMenu()
            }
            else {
                closeSlideMenu()
            }

        }
        func showSlideMenu() {
            self.slideMenuVC?.view.backgroundColor = UIColor.black.withAlphaComponent(0.6)
            self.addChildViewController((slideMenuVC)!)
            self.view.addSubview((slideMenuVC?.view)!)
            AppDelegate.slideMenuBool = false
        }
        func closeSlideMenu() {
            self.slideMenuVC?.view.removeFromSuperview()
            AppDelegate.slideMenuBool = true
        }

1 个答案:

答案 0 :(得分:1)

尝试检查故事板标识符与您的代码标识符匹配。

  slideMenuVC = self.storyboard?.instantiateViewController(withIdentifier: "SlideMenuView") as! SlideMenuView?

如果标识符不匹配,那么您的slideMenuVC将为nil并且异常出现。