【发布时间】:2021-05-26 18:01:36
【问题描述】:
当点击 tabBar 项目时,我正在尝试更改 VC 中的标签文本。但是在我将 UINavigationController 嵌入到 VC 之后,我在点击 tabBar 时崩溃了(线程 1:致命错误:在隐式展开可选值时意外发现 nil)。有人知道如何解决吗? This is a photo of crash line
import UIKit
class TabBarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if tabBarController.selectedIndex == 3{
let navVC = tabBarController.viewControllers![3] as? UINavigationController
let bagPageVC = navVC?.topViewController as? bagPage
bagPageVC!.stringCountOfHP = "\(String(bagPageVC!.countArray)) items in bag"
bagPageVC?.tableView.reloadData()
}
}
}
【问题讨论】:
-
let bagPageVC = navVC.topViewController as? bagPage或tabBarController.viewControllers![3] as! UINavigationController这必须返回nil,添加if let或guard let以进一步调试
标签: ios swift uiviewcontroller uinavigationcontroller uitabbarcontroller