swift - 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导航..请帮忙
解决方案
推荐阅读
- c++ - 哪个优先,自由函数 operator==() 或成员函数 operator==()?
- java - 来自 Observable 的对象
使用 subscribe() 不打印值 - python - 将此损失函数方程转换为python代码
- azure-devops-rest-api - 如何使用 Azure DevOps REST Api 编写构建计划脚本?
- html - 通过网络浏览器单击按钮打开移动相机
- crystal-reports - 如何使用水晶报表图形位置中的基本目录定位图像
- .net - 在 Blazor 和 MVC 中渲染视图
- java - 尝试使用 Spark 数据集 (Java) 查找在 2 个文件之间删除的记录时结果不一致
- jquery - JQuery .click 函数在 Rails 中不起作用(有许多可能不相关的错误)
- ssh-tunnel - 从 tableau desktop 和 tableau server 连接 Aurora Severless