【发布时间】: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