【问题标题】:pushViewController not working properlypushViewController 无法正常工作
【发布时间】:2017-05-01 17:09:52
【问题描述】:

我有一个 SidebarMenueController,它是我启动应用程序后的入口点。 在里面我有一个嵌入了侧边栏(滑出)菜单的 NavigationController。使用 sideBarDidSelectButtonAtIndex 我可以捕捉到应该在 NavigationController 中加载的视图。在侧边栏菜单中选择一个条目时,我想推送到另一个视图。 通过打印(到控制台)选定的菜单条目,我可以确认选择确实有效。但是 pushViewController 函数没有加载所需的视图。 我没有使用故事板! 有关如何解决此问题的任何建议?

import UIKit

class SideBarMenueController: UIViewController, SideBarDelegate {

    var sideBar:SideBar = SideBar()

    lazy var menueButton: UIButton = {
        let button = UIButton()
        button.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        button.backgroundColor = UIColor.clear
        button.setImage(#imageLiteral(resourceName: "MenueIcon"), for: UIControlState.normal)
        button.addTarget(self, action: #selector(didSelectMenueButton), for: UIControlEvents.touchUpInside)
        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        let layout = UICollectionViewFlowLayout()
        let sideBarMenue = UINavigationController(rootViewController:FirstViewController(collectionViewLayout: layout))
        self.addChildViewController(sideBarMenue)
        sideBarMenue.view.frame = self.view.bounds
        self.view.addSubview(sideBarMenue.view)

        UINavigationBar.appearance().barTintColor = UIColor(red: 70/255, green: 174/255, blue: 253/255, alpha: 1)

        let titleAttributes = [
            NSFontAttributeName: UIFont.systemFont(ofSize: 20, weight: UIFontWeightBold),
            NSForegroundColorAttributeName: UIColor.white
        ]
        UINavigationBar.appearance().titleTextAttributes = titleAttributes
        UINavigationBar.appearance().tintColor = UIColor.white

        let NavigationMenueButton = UIBarButtonItem(customView: menueButton)
        navigationItem.leftBarButtonItem = NavigationMenueButton

        sideBar = SideBar(sourceView: self.view, menuItems: ["feed now", "feed times", "cats", "device status", "device setup", "about app"])
        sideBar.delegate = self
    }

    func didSelectMenueButton() {
        let layout = UICollectionViewFlowLayout()
        let controller = FeedTimesController(collectionViewLayout: layout)
        navigationController?.pushViewController(controller, animated: true)
    }

    func sideBarDidSelectButtonAtIndex(_ index: Int) {
        if index == 0{
            let controller = FirstViewController()
            navigationController?.pushViewController(controller, animated: true)
            print("touched index 0")
        } else if index == 1{
            let layout = UICollectionViewFlowLayout()
            let controller = SecondViewController(collectionViewLayout: layout)
            navigationController?.pushViewController(controller, animated: true)
            print("touched index 1")
        } else if index == 2{
            let layout = UICollectionViewFlowLayout()
            let controller = ThirdViewController(collectionViewLayout: layout)
            navigationController?.pushViewController(controller, animated: true)
            print("touched index 2")
        } else if index == 3{
            print("touched index 3")
        } else if index == 4{
            print("touched index 4")
        } else if index == 5{
            print("touched index 5")
        }

        sideBar.showSideBar(false)
    }
}

【问题讨论】:

  • 你介意张贴截图吗?
  • 你指的是哪个具体部分?真正的问题只是通过 pushViewController 从实际视图转换到新视图。
  • 这部分“pushViewController 函数没有加载所需的视图。”它在加载什么?
  • 它什么也没做,视图仍然和以前一样,我跳过了代码,执行了行并将索​​引打印到控制台中,但没有任何变化
  • 说索引 2 被点击。打印语句有效吗?

标签: ios swift uinavigationcontroller pushviewcontroller


【解决方案1】:

看起来您正在创建一个 NavigationController:

let sideBarMenue = UINavigationController(rootViewController:FirstViewController(collectionViewLayout: layout))

然后您将该控制器的视图作为子视图添加到 self.view:

self.view.addSubview(sideBarMenue.view)

但后来,在 didSelect 中,您尝试访问(隐含的)self.navigationController:

navigationController?.pushViewController(controller, animated: true)

不确定,但看起来您想通过以下方式访问导航控制器:

sideBar.navigationController

如果没有看到您用于 SideBar 的代码/库,这可能会也可能不会解决您的问题。

【讨论】:

  • 这不起作用,出现编译器错误。
  • 好吧,我想我们需要更多信息。如果您在sideBarDidSelectButtonAtIndex 中放置一个断点并使用调试工具查看navigationController 实际上是否有效...如果有效(或无效)找出您在 ViewController 层次结构中相对于你创建的 UINavigationController。
猜你喜欢
  • 2016-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-01
  • 2012-07-11
  • 2018-04-08
  • 2017-04-20
  • 2018-10-02
相关资源
最近更新 更多