我正在尝试以编程方式推送ViewController。
代码:
var plus = UIButton()
plus.addTarget(self, action: #selector(plusPressed), for: .touchUpInside)
@objc func plusPressed() {
print("plus")
let createJournalVC = CreateJournalViewController()
self.navigationController?.pushViewController(createJournalVC, animated: true)
}
有效的方法:
什么不起作用:
详细信息
答案 0 :(得分:2)
如果TabBarController在NavigationController之后,则NavigationController可能为nil。您应该先放置TabBarController,然后再将每个与每个选项卡相关的ViewController放入自己的NavigationController中。
故事板:
以编程方式:
您需要像这样创建TabBarController ...
window = UIWindow(frame: UIScreen.main.bounds)
let tabCon = UITabBarController()
let navCon1 = UINavigationController(rootViewController: ViewController())
let navCon2 = UINavigationController(rootViewController: CreateJournalViewController())
let navCon3 = UINavigationController(rootViewController: AnotherViewController())
tabCon.viewControllers = [navCon1, navCon2, navCon3]
tabCon.tabBar.items?[0].title = NSLocalizedString("VC", comment: "comment")
tabCon.tabBar.items?[1].title = NSLocalizedString("CJV", comment: "comment")
tabCon.tabBar.items?[2].title = NSLocalizedString("AVC", comment: "comment")
window?.rootViewController = tabCon
window?.makeKeyAndVisible()
答案 1 :(得分:0)
似乎navigationController
是nil
,这就是为什么不推送视图控制器的原因。