简体   繁体   English

SideMenu didSelect方法在swift中不起作用

[英]SideMenu didSelect method is not working inSwift

I am using LGSideMenuController pod in app to show the side menu on my home screen 我在应用程序中使用LGSideMenuController窗格在主屏幕上显示侧边菜单

I created a function in my AppDelegate file like this to show the side menu 我在这样的AppDelegate文件中创建了一个函数,以显示侧面菜单

func rootViewController() {

    let appDelegate = UIApplication.shared.delegate as? AppDelegate

    let mainViewController = DashboardViewController.instantiate(fromAppStoryboard: .Main)
    let leftViewController = SideMenuViewController.instantiate(fromAppStoryboard: .Main)

    let sideMenuController = LGSideMenuController(rootViewController: UINavigationController(rootViewController: mainViewController),leftViewController: leftViewController, rightViewController: nil)
    sideMenuController.delegate = self
    sideMenuController.leftViewWidth = UIScreen.main.bounds.width - 150
    sideMenuController.leftViewPresentationStyle = .slideAbove
    self.navigationController?.pushViewController(sideMenuController, animated: true)
    appDelegate?.window?.rootViewController = sideMenuController
    appDelegate?.window?.makeKeyAndVisible()
}

And call it in my LoginViewController to set the rootViewController in the app 并在我的LoginViewController调用它以在应用程序中设置rootViewController

@IBAction func signInButtonTapped(_ sender: UIButton) { 
    AppDelegate.shared.rootViewController()
}

After that I called sideMenu in my DashboardViewController 之后,我在DashboardViewController调用sideMenu

import UIKit
import LGSideMenuController

class DashboardViewController: UIViewController, LGSideMenuDelegate {


    //MARK:- IBOUTLETS
    //MARK:-
    @IBOutlet weak var collectionView: UICollectionView!

    //MARK:- VARIABLES
    //MARK:-
    var dashboardItems =  ["Customer", "Sales Men", "Order", "Payment Collection", "News", "Message", "demo", "demo", "demo", "demo"]

    //MARK:- CONSTANTS
    //MARK:-


    //MARK:- VIEW CONTROLLER LIFE CYCLE
    //MARK:-
    override func viewDidLoad() {
        super.viewDidLoad()

        sideMenuController?.delegate = self
        addHamburgerMenu()
    }

    override func onHamburgerMenu() {
        self.sideMenuController?.showLeftView(animated: true, completionHandler: nil)
    }

}

SideMenuController class: SideMenuController类:

import UIKit
import XLPagerTabStrip
import LGSideMenuController

class SideMenuViewController: UIViewController, IndicatorInfoProvider, LGSideMenuDelegate {

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        sideMenuController?.delegate = self
    }

    func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo { 
        return IndicatorInfo(title: "SideMenu")
    }
}


extension SideMenuViewController: UITableViewDataSource, UITableViewDelegate {

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

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = self.tableView.dequeueReusableCell(withIdentifier: "sideMenuCell", for: indexPath) as! SideMenuTableViewCell
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        print("did select is working fine")
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyBoard.instantiateViewController(withIdentifier: "CustomerListViewController") as! CustomerListViewController
        self.navigationController?.pushViewController(vc, animated: true)
    }

}

class SideMenuTableViewCell: UITableViewCell {

    @IBOutlet weak var itemLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib() 
    }
}

when I am clicked side button it is showing the sideMenu but the problem is when I am selecting any item in sideMenu to go to next ViewController it is not working. 当我单击侧面按钮时,它显示了sideMenu,但是问题是当我在sideMenu中选择任何一项去下一个ViewController时,它不起作用。 When I am clicking sendNewRequest it is not working. 当我单击sendNewRequest时,它不起作用。 Please help? 请帮忙?

User following code to fix your issue in tableView's didSelect. 用户使用以下代码来修复tableView的didSelect中的问题。

let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyBoard.instantiateViewController(withIdentifier: "CustomerListViewController") as! CustomerListViewController
let navigationController = UINavigationController(rootViewController: viewController)
sideMenuController?.rootViewController = navigationController
sideMenuController?.rootView = navigationController.view
sideMenuController?.hideLeftView(animated: true, completionHandler: nil)

In your current view controller add following method and call in viewDidLoad() 在当前的视图控制器中,添加以下方法并调用viewDidLoad()

func addMenuButton() {
    let button = UIButton(type: .custom)
    button.setImage(#imageLiteral(resourceName: "icon_menu").withRenderingMode(.alwaysTemplate), for: UIControlState.normal) //icon_menu is icon of menu in assets.
    button.tintColor = .white
    button.addTarget(self, action:#selector(showMenu), for:.touchUpInside)
    button.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
    let menuButton = UIBarButtonItem(customView: button)
    self.navigationItem.leftBarButtonItem = menuButton
}

Add button's target method to show menu again. 添加按钮的目标方法以再次显示菜单。

@objc func showMenu() {
    sideMenuController?.showLeftViewAnimated()
}

This is working code in my current project. 这是我当前项目中的工作代码。 I hope it will help you. 希望对您有帮助。

I think problem in UINavigationController . 我认为UINavigationController问题。

The navigation not working 导航不起作用

let objDetail = self.storyboard?.instantiateViewController(withIdentifier: "DetailVC") as! DetailVC?
let mainViewController = sideMenuController!
let navigationVC = mainViewController.rootViewController as! UINavigationController
navigationVC.pushViewController(objDetail!, animated: true)

You need to change didSelectRowAt method in SideMenuViewController: 您需要在SideMenuViewController中更改didSelectRowAt方法:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let storyBoard = UIStoryboard(name: "Main", bundle: nil)
    let viewController = storyBoard.instantiateViewController(withIdentifier: "CustomerListViewController") as! CustomerListViewController

    let navigationController = sideMenuController!.rootViewController as! UINavigationController
    navigationController.pushViewController(viewController, animated: true)

    sideMenuController?.hideLeftView(animated: true, completionHandler: nil)
}
    self.navigationController?.pushViewController(vc, animated: true)

It's because of this line, you are telling it to pushViewController into a nil navigationController , 因为这一行,您要告诉它将pushViewController转换为nil navigationController

if you print out self.navigationController while debugging you would find that it's nil . 如果在调试时打印出self.navigationController ,则会发现它为nil

Solution

1- You can create a navigationController simply append the desired UIViewController to it and present that navigationController however this is not preferred for your case. 1-您可以创建一个navigationController只需将所需的UIViewController附加到其上,并显示该navigationController但是对于您的情况,这不是首选。

2- You should get the storyboard identifier of your main navigation controller and instantiate that and then push into it. 2-您应该获取主导航控制器的情节提要板标识符并将其实例化,然后推入其中。

        let nav = self.storyboard?.instantiateViewController(withIdentifier: "YourNavID") as! UINavigationController
        let vc = storyBoard.instantiateViewController(withIdentifier: "CustomerListViewController") as! CustomerListViewController
        nav.pushViewController(vc, animated: false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM