ios - 从其他 subview 加载 ViewController 作为主视图中的 subview

标签 ios swift3 xcode8

我正在使用 swift3/xcode 8 编写一个应用程序。我的应用程序有一个中央 View Controller ,称为 MenuViewController,它负责侧边菜单。 在这个里面,我将其他 View 加载为 subview ,这样我就可以在所有其他 View 中使用侧边菜单。

但我现在有疑问:在一个特定的 View Controller 中,我有一个 TableView ,它有一个方法来执行选择项目的操作。这个 Action 调用其他 查看并传递参数。

那么,如何使用此参数将此 View 加载到 BaseViewController 中?可能吗?

如果不清楚请告诉我......

谢谢!

更新 1:

我正在使用一个我觉得很有趣的解决方案: Cocoacasts

基本上我在 MenuViewController 上有这段代码:

在 MenuViewController 中声明要加载的 View :

    lazy var filamentoViewController: FilamentoViewController = {

        let storyboard = storyboard.instantiateViewController(withIdentifier: "FilamentoViewController") as! FilamentoViewController 
        self.addViewControllerAsChildViewController(childViewController: viewController)
        return viewController
    }()

在 MenuViewController 中添加 View Controller 作为 subview :

    private fun addViewControllerAsChildViewController(childViewController: UIViewController)
    {
      addChildViewController(childViewController)
      view.addSubView(childViewController.view)
      childViewController.view.frame = view.bounds
      childViewController.view.autoResizingMask = [.flexibleWidth, .flexibleHeight]
      childViewController.didMove(toParentViewController: self)
    }

FilamentoViewController 中调用其他 View Controller 的代码:

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath)
    {
      let editFilamentoViewController:EditFilamentoViewController = EditFilamentoViewController()
      editFilamentoViewController.cod = indexPath.row
      self.presentViewController(editFilamentoViewController, animated: true, completion: nil)
    }

代码

      self.presentViewController(editFilamentoViewController, animated: true, completion: nil)

是我想要加载到 MenuViewController 中的那个。

更新 2:

MenuViewController 是一个 CentreViewController。我在中间加载其他 View ,菜单来自左侧。

最佳答案

有一个内置 Controller 可以满足您的需求(即 UISplitViewController),您考虑过吗?

Apple documentation

Sample Tutorial

还有滑出式菜单库。

slide out menu

关于ios - 从其他 subview 加载 ViewController 作为主视图中的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41695929/

相关文章:

iOS Facebook 登录 "Given URL is not allowed by the Application configuration"

swift - 无法向 UITableView 中的部分添加行

swift - 如何指定输出可执行文件的名称?

ios - AFNetworking 架构 i386 的 undefined symbol : "_OBJC_CLASS_$_AFHTTPSessionManager"

ios - TableView 不显示来自 firebase 的数据,但在 "po snapshot"调试控制台下显示获取数据

ios - 重用自定义 tableViewCell 单元格中的隐藏按钮 [ios]

ios - Xcode 8 无法在设备上运行,配置文件问题提到 Apple Watch

ios - 我不知道我的代码有什么问题(带有 MWFeedParser 的 rss 阅读器)

javascript - 如何在 React Native 中将事件从 iOS 发送到 javascript

ios - 如何从 UITabBarViewController 获取对 View Controller 的引用