使用 swrevealviewcontroller 从 uitableviewcell 导航到多个 uinavigationcontroller

navigating from uitableviewcell to multiple uinavigationcontroller using swrevealviewcontroller

我是 swift 语言的新手,开始创建具有滑动菜单的存根应用程序。我使用 http://www.appcoda.com/sidebar-menu-swift/ 中的教程来创建幻灯片菜单,但想创建动态而非静态的应用程序,如示例所示。我在创建 segue 或从 uitableviewcell 导航到连接到相应 uinavigationcontroller 的相应 uiviewcontrollers 时遇到问题。

滑动菜单代码如下class:

MenuController.swift

import UIKit

class MenuController:UITableViewController
{
    let menuControlList = ["Category 1", "Category 2", "Category 3", "Category 4"]

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // Return the number of sections.
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Return the number of rows in the section.
        return menuControlList.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! MenuTableCell

        let row  = indexPath.row
        cell.menuCellText.text = menuControlList[row]
        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("row clicked :: ", indexPath.row)

        switch indexPath.row
        {
        case 0:
            var cat1View = Category1(nibName:"Category1", bundle:nil)
            self.navigationController?.pushViewController(cat1View, animated: true)
            print(indexPath.row)
            break

        case 1:
            var cat2View = Category2(nibName:"Category2", bundle:nil)
            self.navigationController?.pushViewController(cat2View, animated: true)
            print(indexPath.row)
            break

        case 3:
            var cat3View = Category3(nibName:"Category3", bundle:nil)
            self.navigationController?.pushViewController(cat3View, animated: true)
            print(indexPath.row)
            break

        case 4:
            var cat4View = Category4(nibName:"Category4", bundle:nil)
            self.navigationController?.pushViewController(cat4View, animated: true)
            print(indexPath.row)
            break

        default:
            return;
        }
    }
}

以下是我的故事板截图:

如果我在创建这个过程中犯了任何错误,请告诉我并帮助我纠正它。

以下是我的类别 1 class 的代码:

class Category1 :UIViewController
{

    @IBOutlet var menuBtn: UIBarButtonItem!


    override func viewDidLoad() {
        super.viewDidLoad()

        if self.revealViewController() != nil {
            menuBtn.target = self.revealViewController()
            menuBtn.action = "revealToggle:"
            self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }   
}

编辑:

尝试了以下解决方案:

let vc = storyboard.instantiateViewControllerWithIdentifier("Category1") as! Category1
presentViewController(vc, animated: true, completion: nil)

上面的代码直接打开带有滑动动画的类别 1 viewcontroller 但它不是通过相应 viewcontroller.

附带的导航控制器打开的

如果我使用以下代码:

let vc = storyboard.instantiateViewControllerWithIdentifier("Category1") as! Category1
self.revealViewController().setFrontViewController(vc, animated: true)

上面的代码也加载了 viewcontroller 但滑动菜单没有滑回?

首先,为您的导航视图控制器提供故事板 ID,而不是实际的视图控制器。因此,例如,如果 Category1 嵌入在导航控制器中,则为导航控制器提供故事板 ID,例如 Category1NavController。那么你的代码应该如下:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    print("row clicked :: ", indexPath.row)

    switch indexPath.row
    {
    case 0:
        let navController1 = storyboard.instantiateViewControllerWithIdentifier("Category1NavController") as! UINavigationController 
        self.revealViewController().setFrontViewController(navController1, animated: true)
        print(indexPath.row)
        break
    ..........
    // Handle other cases similarly here
    case 1:
    .....
    case 2:
    .....   
    default:
        return;
    }
    self.revealViewController().setFrontViewPosition(FrontViewPosition.Left, animated: true)
}

您还应该在 switch 语句结束后重置顶视图位置。它应该是这样的(如果语法不正确,请再次抱歉,请相应地修复):

self.revealViewController().setFrontViewPosition(FrontViewPosition.Left, animated: true)

必须在Swift中调用类似的方法。

希望这对您有所帮助。请原谅我的 SWIFT 语法并在需要时更正它。