pushViewController无法正常工作

时间:2017-05-01 17:09:53

标签: ios swift uinavigationcontroller pushviewcontroller

我有一个SidebarMenueController,这是我启动应用程序后的入口点。 在那里,我有一个嵌入了侧边栏(滑出)menue的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)
    }
}

1 个答案:

答案 0 :(得分:0)

看起来您正在创建一个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的代码/库,这可能会也可能不会解决您的问题。