SideMenu didSelect方法在swift中不起作用

时间:2019-01-09 12:59:04

标签: ios swift

我在应用中使用LGSideMenuController窗格在主屏幕上显示侧边菜单

我在 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()
}

并在我的 LoginViewController 中调用它,以在应用中设置rootViewController

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

此后,我在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类:

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() 
    }
}

当我单击侧面按钮时,它显示了sideMenu,但是问题是当我在sideMenu中选择任何项转到下一个ViewController时,它不起作用。当我单击sendNewRequest时,它不起作用。请帮忙吗?

4 个答案:

答案 0 :(得分:1)

用户使用以下代码修复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)

在当前的视图控制器中,添加以下方法并调用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
}

添加按钮的目标方法以再次显示菜单。

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

这是我当前项目中的有效代码。希望对您有帮助。

答案 1 :(得分:1)

我认为UINavigationController中存在问题。

导航不起作用

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

答案 2 :(得分:1)

您需要在SideMenuViewController中更改didSelectRowAt方法:

import matplotlib.pyplot as plt

plt.scatter(x,y, c='b', marker='x', label='1')
plt.scatter(x, y, c='r', marker='s', label='-1')
plt.legend(loc='upper left')
plt.show()

答案 3 :(得分:0)

    self.navigationController?.pushViewController(vc, animated: true)

由于这一行,您正在将其告诉pushViewControllernil navigationController

如果在调试时打印出self.navigationController,则会发现它是nil

解决方案

1-您可以简单地将所需的navigationController附加到其上并显示UIViewController来创建navigationController,但是对于您的情况,这不是优选的。

2-您应该获取主导航控制器的情节提要标识符,并将其实例化然后推入。

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