gpt4 book ai didi

ios - PushViewController 无法正常工作

转载 作者:行者123 更新时间:2023-11-30 12:29:40 26 4
gpt4 key购买 nike

我有一个 SidebarMenueController,它是启动应用程序后的入口点。在其中我有一个嵌入侧边栏(滑出)菜单的导航 Controller 。使用 sideBarDidSelectButtonAtIndex 我捕获哪个 View ,该 View 应该加载到 NavigationController 中。当在侧边栏菜单中选择一个条目时,我想推送到另一个 View 。通过打印(到控制台)所选的菜单条目,我可以确认该选择确实有效。但是pushViewController函数没有加载所需的 View 。我没有使用 Storyboard!关于如何解决这个问题有什么建议吗?

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)
}
}

最佳答案

看起来您正在使用以下内容创建一个 NavigationController:

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

然后您将该 Controller 的 View 作为 subview 添加到 self.view:

self.view.addSubview(sideBarMenue.view)

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

navigationController?.pushViewController(controller, animated: true)

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

sideBar.navigationController

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

关于ios - PushViewController 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43723204/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com