试图在IOS中创建一个Slidebar菜单

时间:2017-04-02 02:21:27

标签: ios user-interface swrevealviewcontroller

我想在我的应用中创建一个滑动条菜单(如下图所示) enter image description here

当我点击该汉堡图标时,它应显示如下所示的菜单。 enter image description here

我目前正在使用SWRevealController,一个用Objective C编写的lib。我创建了一个桥接头并导入了该类。这是我目前的设置 enter image description here

我的初始视图分支到菜单控制器或UIViewTable是SWRevealController,而segue是SWRevealViewControllerSegueSetController的子类。在我的食物项目视图控制器(存在汉堡包导航的控制器)中,我添加了以下代码来激活滑动条动作。

override func viewDidLoad() {
        super.viewDidLoad()

        //Creating the slidebar Navigation using SWRevealViewController 
        if self.revealViewController() != nil {
            menuButton.target = self.revealViewController()
            menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
            self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }

}

但是,当我点击按钮时,它仍然没有响应。当我用我的其他应用程序执行此操作时,它运行正常。我不确定为什么它不在这里工作

这是我用作参考的来源:http://www.appcoda.com/sidebar-menu-swift/

查看控制器完整代码

@IBOutlet weak var thumbnail_image: UIImageView!
    //@IBOutlet weak var food_detail: UITextView!
    @IBOutlet weak var testing: UILabel!

    @IBOutlet weak var menuButton: UIBarButtonItem!
    var jsonResponseFood: NSArray = NSArray() //API for types of food available
    var jsonResponseRecipe: NSArray = NSArray() // API for recipes in food
    var selectedFood: foodItem?
    var postStr: String?

    //Initializing API
    let FullRecipeAPIModel = fullRecipeAPIModel()

    override func viewDidLoad() {
        super.viewDidLoad()

        //Creating the slidebar Navigation using SWRevealViewController 
        if self.revealViewController() != nil {
            menuButton.target = self.revealViewController()
            menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
            self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }


        print(selectedFood?.name as String!)
        //testing.text = self.selectedFood?.name

        let APIModel = apiModel()

        APIModel.delegate = self
        FullRecipeAPIModel.delegate = self
        APIModel.downloadItems(postString: postStr!)

        // Do any additional setup after loading the view.
    }

谢谢StackOverflow。

2 个答案:

答案 0 :(得分:0)

您的代码中的问题是您没有将SWRevealController设置为root视图。没有通过代码方式提供的Storyboard选项,我们必须再次进行配置。这个库是这样设计的。因此,如果您更改下面的代码,它将像魅力一样工作。

if(stat == "login"){
                if(responseString == "invalid"){
                    self.isValid = false;
                    print(self.isValid)
                }

                DispatchQueue.main.async(execute: {
                    if self.checkLogin(data: responseString!) == true {
                        let storyboard = UIStoryboard(name: "food", bundle: nil)
                        let controller = storyboard.instantiateViewController(withIdentifier: "foodItemViewController") as! foodItemViewController
                        let navController = UINavigationController(rootViewController: controller) // Creating a navigation controller with VC1 at the root of the navigation stack.
                        let appDelegate = UIApplication.shared.delegate as! AppDelegate
                        let menuView :MenuController = storyboard.instantiateViewController(withIdentifier: "menuBar") as! MenuController
                        let revealController = SWRevealViewController()

                        revealController.frontViewController = navController;
                        revealController.rearViewController = menuView;

                        appDelegate.window?.rootViewController = revealController

                    }
                    else{
                        //Show alert here
                        self.showAlert(title: "User Does Not Exist", alertTitle: "Close", message: "");
                    }
                });
            }
            else{
                //CODE COULD BE SIMPLIFIED
                DispatchQueue.main.async(execute: {
                    if(responseString == "duplicate"){
                        //Show alert here
                        self.showAlert(title: "User with this email already exist", alertTitle: "Close", message: "Please register with another email address")
                    }
                    else{
                        //Show Food UITable
                        let storyboard = UIStoryboard(name: "food", bundle: nil)
                        let controller = storyboard.instantiateViewController(withIdentifier: "entranceFoodController") 
                        /*let navController = UINavigationController(rootViewController: controller) // Creating a navigation controller with VC1 at the root of the navigation stack.*/
                        let appDelegate = UIApplication.shared.delegate as! AppDelegate

                        let menuView :MenuController = storyboard.instantiateViewController(withIdentifier: "menuBar") as! MenuController
                        let revealController = SWRevealViewController()

                        revealController.frontViewController = controller;
                        revealController.rearViewController = menuView;

                        appDelegate.window?.rootViewController = revealController                    }

                });
            }

答案 1 :(得分:0)

我今天也正在研究这个问题,这是使用SWRevealController的最佳选择,是不是苹果公司已经建立了这样做的方式?我渴望看到选项而不是从Github那里获得一些东西,除非那是我最后的选择。