首页 > 解决方案 > Sidemenu 控制器菜单无法快速导航到相应的视图控制器(请帮助)

问题描述

我在名为 MenuVC 的 tableview 中创建了 sidemenu ..

我已经创建了带有如下菜单按钮的自定义导航栏,并在每个视图控制器中调用

func setUpNavigationBar(){
    
    let menuImageView = UIImageView.init(image: #imageLiteral(resourceName: "menu8"))
    menuImageView.tintColor = .darkGray
    menuImageView.frame = CGRect(x:0.0,y:0.0, width:25,height:25.0)
    menuImageView.contentMode = .scaleAspectFit
    menuImageView.widthAnchor.constraint(equalToConstant: 25).isActive = true
    menuImageView.heightAnchor.constraint(equalToConstant: 25).isActive = true
    menuImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(menuClick)))
    let btnMenu = UIBarButtonItem(customView: menuImageView)
    
    self.navigationItem.setLeftBarButton(btnMenu, animated: true)

  }
  @objc func menuClick(){
    print("menu revealed: \(self.sideMenuController?.isMenuRevealed)")
    if !(self.sideMenuController?.isMenuRevealed ?? false){
        self.sideMenuController?.revealMenu()
    }
  }

在场景中

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

let menu = (StoryBoard.main).instantiateViewController(identifier: "MenuVC") as! MenuVC
window?.rootViewController = SideMenuController(contentViewController: mainVC, menuViewController: menu)
self.window?.makeKeyAndVisible()
}

在 MenuVC 中:

struct MenuModel{
var name : String?
var icon : UIImage?
var isSelect : Bool?
}

class MenuVC: UIViewController,UITableViewDelegate,UITableViewDataSource{



var arrMenu = [MenuModel(name: "Home", icon: nil, isSelect: true),
               MenuModel(name: "All Products", icon: nil, isSelect: false),
               MenuModel(name: "Shop by category", icon: nil, isSelect: false)]


func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrMenu.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MenuTableCell", for: indexPath) as! MenuTableCell

        cell.lblTitle.text = arrMenu[indexPath.row].name
    
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    
    DispatchQueue.main.async {
        let from  = (self.sideMenuController?.contentViewController as? UINavigationController)
        if indexPath.section == 0{
            for i in 0..<self.arrMenu.count{self.arrMenu[i].isSelect = false}
            self.arrMenu[indexPath.row].isSelect = true
            tableView.reloadData()
            (self.sideMenuController?.isMenuRevealed ?? false) ? self.sideMenuController?.hideMenu() : nil
            switch indexPath.row {
            case 0:
                let signupVC = StoryBoard.main.instantiateViewController(identifier: "HomeVC") as! HomeVC
                self.navigationController?.pushViewController(signupVC, animated: true)
                break
            case 1:
                let signupVC = StoryBoard.main.instantiateViewController(identifier: "AllProductsVC") as! AllProductsVC
                self.navigationController?.pushViewController(signupVC, animated: true)
                break
            case 2:
                let signupVC = StoryBoard.main.instantiateViewController(identifier: "CategoryVC") as! CategoryVC
                self.navigationController?.pushViewController(signupVC, animated: true)
                break
            default:
                return
            }
        }else{
           
        }
    }

使用代码我可以打开菜单但是如果我点击All Products菜单然后它不会去那个viewcontroller..我应该如何从sidemenu导航..请帮忙

标签: swiftuinavigationcontrollerside-menu

解决方案


推荐阅读