For existing UITabBarController already in storyboard do have Two TabBars (Home / Settings).
Now I added two more TabBarIems based on conditions based
(programVC / reportVC) i.e., UIViewController.
When Switching Tab programVC or reportVC app get crashed.
The @IBOutlet var skeletonView: UIView! added in ProgramReportVC set to nil
class MainTabbarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
addedConditionalTabs(isInserted: true)
}
// For existing tab inserted new tabs based on condition
func addedConditionalTabs(isInserted: Bool){
if isInserted{
let programVC = ProgramReportVC() // ProgramReportVC -> UIViewController
programVC = “Program”
let reportVC = ProgramReportVC()
reportVC.screenTitle = “Report”
programVC.tabBarItem = UITabBarItem(title: "Program", image: UIImage(named: “ProgramImage”), tag: 2)
reportVC.tabBarItem = UITabBarItem(title: "Report", image: UIImage(named: “ReportImage”), tag: 3)
self.viewControllers?.insert(programVC, at: 2)
self.viewControllers?.insert(reportVC, at: 3)
}
}
s
Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
How to resolved Unexpectedly found nil ?
programVC ->
@IBOutlet var skeletonView: UIView!
@IBOutlet var views: [UIView]!
@IBOutlet var subviews: [UIView]!
override func viewDidLoad() {
super.viewDidLoad()
self.showSkeleton()
}
func showSkeleton() {
self.skeletonView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
self.view.addSubview(self.skeletonView)
self.view.bringSubviewToFront(self.skeletonView)
let gradient = SkeletonGradient(baseColor: UIColor(named: "skeletonColor")!)
let animation = SkeletonAnimationBuilder().makeSlidingAnimation(withDirection: .leftRight)
self.skeletonView.isSkeletonable = true
for items in views {
items.isSkeletonable = true
}
for items in subviews {
items.isSkeletonable = true
items.showAnimatedGradientSkeleton(usingGradient: gradient, animation: animation)
}
}
It sounds like this is unrelated to the UITabBarController or how you're adding VCs to it. If a
UIViewthat isnilgets force unwrapped (as is the case with yourskeletonViewIBOutlet), that will cause a crash regardless of whether the VC is presented by a tab controller or by something else.Update: Based on the thread conversation, your issue is that you're accessing
skeletonViewinside of theProgramReportVC'sviewDidLoadmethod. The VC's view will not have been initialized at that point in time, which also means its IBOutlets will not have been resolved yet.My recommendation would be to call
showSkeleton()inviewDidAppearinstead, since UI updates in general are best done once the UI is actually visible on the screen.