检测哪个 viewController 呈现了 SideMenu 并为其呈现实现适当的功能

Posted

技术标签:

【中文标题】检测哪个 viewController 呈现了 SideMenu 并为其呈现实现适当的功能【英文标题】:Detect which viewController presented the SideMenu and implement proper functionality for its presentation 【发布时间】:2019-02-18 16:21:42 【问题描述】:

我使用the following pod 来实现我的 SideMenu 功能。现在,如果我从 XYZ viewController 打开它并选择再次打开 XYZ viewController 的行,则页面正在被推送,但相反,我希望 SideMenu 被关闭,而不是推送已经呈现的 viewController。

这是用户界面:

这是我的 didSelectRow 代码,非常清楚:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
    switch indexPath.row 
    case 0:
        let playerVC = NavigationHelper.shared.getStoryBoard().instantiateViewController(withIdentifier: "mainPage") as? MainViewController
        navigationController?.pushViewController(playerVC!, animated: true)
    case 1:
        let historyVC = NavigationHelper.shared.getStoryBoard().instantiateViewController(withIdentifier: "historyPage") as? BroadcastsHistoryViewController
        navigationController?.pushViewController(historyVC!, animated: true)
    case 2:
        let sendMessageVC = NavigationHelper.shared.getStoryBoard().instantiateViewController(withIdentifier: "messagingPage") as? MessaginViewController
        navigationController?.pushViewController(sendMessageVC!, animated: true)
    case 3:
        let settingsVC = NavigationHelper.shared.getStoryBoard().instantiateViewController(withIdentifier: "settingPage") as? SettingsViewController
        navigationController?.pushViewController(settingsVC!, animated: true)
    case 4:
        let aboutVC = NavigationHelper.shared.getStoryBoard().instantiateViewController(withIdentifier: "aboutPage") as? AboutAppViewController
        navigationController?.pushViewController(aboutVC!, animated: true)
    default: break
    

到目前为止,我已经尝试检测topMost viewController,获取presentingViewController 属性,但没有成功。谁能帮我以适当的方式处理它?

【问题讨论】:

【参考方案1】:

尝试将var previousVC: UIViewController? 添加到 sideMenu 根视图控制器(我们称之为 SideMenuVC)

然后在每个视图控制器表单上,您将从 add 显示侧边菜单

override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
    if let nextVC = segue.destination as? SideMenuVC 
        nextVC.previousVC = self
    

然后在XYZViewController案例上写

if let vc = previousVC as? XYZViewController
    //dismiss sidemenu
 else 
   //instantiate and push ViewController 

【讨论】:

以上是关于检测哪个 viewController 呈现了 SideMenu 并为其呈现实现适当的功能的主要内容,如果未能解决你的问题,请参考以下文章

检测ViewController是否显示为FormSheet

如何检测快速点击了哪个图像

检测按下哪个按钮来打开此视图[重复]

iOS:具有透明背景的模态 ViewController

从 View 与 ViewController 对话

如何在 Xamarin for iOS 中呈现具有不同导航的 viewController