无法在侧边菜单中进行转场

Posted

技术标签:

【中文标题】无法在侧边菜单中进行转场【英文标题】:Can't make a segue in Side Menu 【发布时间】:2019-01-24 07:15:57 【问题描述】:

我正在使用this Side Menu 窗格,我无法从此菜单中进行转场。在我的 Storyboard 上,我有(初始)ViewController(MainViewController)、带有 RootController 的 NavigationController 和 ViewController(GameController):

我创建了从 RootController 到 GameController 的 Segue 并安装了 Identifier: "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
        
    

什么也没发生:

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

详情:

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
    

【问题讨论】:

ShowGamesegue? 的标识符 @iPeter RootController -> GameViewController 我刚刚实现了,已经成功了,你能实现他们说的github的所有流程吗? @MrTishman 放置一个断点,看看哪个案例被调用。 你能上传RootController代码吗? 【参考方案1】:

这个Demo将帮助你实现:-

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)
    


【讨论】:

如果您按照他们在 SideMenu Github 中所说的正确完成了故事板部分,请尝试使用此代码。因为我实现了它是可运行的,如果您想从头开始上传步骤,它们在 SideMenu 库中显示的内容我上传了吗? 不,我所做的一切都是正确的,但这段代码对我也不起作用。我在新项目上对其进行了测试。 没什么,和主题中的 GIF 一样。 可能是我没有在 Storyboard 中做一些细节【参考方案2】:

尝试使用 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)

【讨论】:

谢谢,但我找到了解决方案。将发布它。【参考方案3】:

我找到了解决方案here。

快速回答:

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

感谢@Abhishek Jadhav的帮助,您的代码运行良好!

【讨论】:

以上是关于无法在侧边菜单中进行转场的主要内容,如果未能解决你的问题,请参考以下文章

由于没有标识符错误的转场,无法执行转场

使用 didSelectRowAtIndexPath 进行转场

Vegas怎么设置转场特效中的关键帧动画?

无法执行转场 [重复]

无法执行转场

通过点击单元格中的图像视图进行转场[重复]