无法在侧边菜单中进行选择

时间:2019-01-24 07:15:57

标签: ios swift uikit

我正在使用this侧面菜单窗格,无法从该菜单进行选择。在我的情节提要中,我有一个(初始)ViewController(MainViewController),带有RootController的NavigationController和另一个ViewController(GameController):

Storyboard

我从RootController到GameController创建了Segue,并安装了标识符:“ ShowGame”,然后为segue编写了一些代码:

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.row {
        case 0:
            tableView.deselectRow(at: indexPath, animated: true)
            break
        case 1:
            self.performSegue(withIdentifier: "ShowGame", sender: self)
            tableView.deselectRow(at: indexPath, animated: true)
        case 2:
            self.performSegue(withIdentifier: "ShowGame", sender: self)
            tableView.deselectRow(at: indexPath, animated: true)
        default:
            break
        }
    }

什么也没发生:

Example

我不明白为什么它不起作用,对于以前的项目(没有侧面菜单),这种方式是完美的,但是现在我遇到了麻烦。请帮忙。

详细信息:

details

RootController代码:

import UIKit

class MenuViewController: UITableViewController, UINavigationControllerDelegate {
    var webServer = GCDWebServer()

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.tableFooterView = UIView()
        self.navigationController?.delegate = self
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 3
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let menuCell = tableView.dequeueReusableCell(withIdentifier: "MenuCell") as? MenuTableViewCell
        switch indexPath.row {
        case 0:
            menuCell?.updateCell(title: "Главная")
        case 1:
            menuCell?.updateCell(title: "Первая игра")
        case 2:
            menuCell?.updateCell(title: "Вторая игра")
        default:
            break
        }
        return menuCell!
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.row {
        case 0:
            tableView.deselectRow(at: indexPath, animated: true)
            break
        case 1:                
            self.performSegue(withIdentifier: "ShowGame", sender: self)
            tableView.deselectRow(at: indexPath, animated: true)
        case 2:
            self.performSegue(withIdentifier: "ShowGame", sender: self)
            tableView.deselectRow(at: indexPath, animated: true)
        default:
            break
        }
    }
}

MenuCell代码:

import UIKit

class MenuTableViewCell: UITableViewCell {

    @IBOutlet weak var menuTitle: UILabel!

    func updateCell(title: String) {
        menuTitle.text = title
    }
}

3 个答案:

答案 0 :(得分:1)

此演示将帮助您实现:-

class MenuViewController: UITableViewController {

    var menus = ["Menu1", "Menu2", "Menu3"]

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


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

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

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

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MenuCell") as? MenuTableViewCell
        cell.menuTitle?.text = menus[indexPath.row]
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.row {
        case 0:
            let firstMenuVC = self.storyboard?.instantiateViewController(withIdentifier: "FirstMenuViewController") as! FirstMenuViewController
            self.navigationController?.pushViewController(firstMenuVC, animated: true)
        case 1:
            let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
            self.navigationController?.pushViewController(secondVC, animated: true)
        case 2:
            let thirdVC = self.storyboard?.instantiateViewController(withIdentifier: "ThirdViewController") as! ThirdViewController
            self.navigationController?.pushViewController(thirdVC, animated: true)
        default:
            break
        }
        tableView.deselectRow(at: indexPath, animated: true)
    }

}

答案 1 :(得分:0)

尝试使用push而不是segue看起来不错

let yourVcObject : AnyOutof3Controllers = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AnyIdentifier") as! YourVc
        self.dismiss(animated: false) {
            if let topController = UIApplication.topViewControllerForNav() as? UINavigationController {
                topController.visibleViewController?.navigationController?.pushViewController(AnyOutof3Controllers)

AnyOutof3Controllers表示任何控制器的名称:例如,如果您单击第二个,则必须使用2ndViewControll代替此(AnyOutof3Controllers)

答案 2 :(得分:0)

我找到了解决方法here

快速解答:

故事板必须看起来像“导航控制器-> [主视图控制器]->(模态)侧面菜单导航控制器->(嵌入式/根视图控制器关系)表视图控制器(带有菜单选项)->(推动)其他视图控制器”

example

感谢@@ strong> Abhishek Jadhav 的帮助,您的代码可以正常工作!