the crash report can see below:
crash report
Post
Replies
Boosts
Views
Activity
Hi, After research, we have found that the crash appear in iOS 18 and below iOS 18, it works well.
And I write a simple demo that can reproduce it.
The code is:
AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let window = UIWindow()
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let tabBarViewController = mainStoryboard.instantiateViewController(withIdentifier: "tabBarVC") as! TabBarViewController
tabBarViewController.rebuildTabBar()
window.rootViewController = tabBarViewController
self.window = window
window.makeKeyAndVisible()
return true
}
TabBarViewController:
class TabBarViewController: UITabBarController {
private let vc1 = UIViewController()
private let vc2 = UIViewController()
private let vc3 = UIViewController()
override func viewDidLoad() {
super.viewDidLoad()
// not crash
setViewControllers([vc1], animated: false)
//will crash
setViewControllers([vc2], animated: false)
}
public func rebuildTabBar() {
setViewControllers([vc1, vc2, vc3], animated: false)
}
}
We use UIStoryboard to create a TabBarViewController instance, ViewController use UIStoryboard will not call viewDidLoad at the beginning, and after call UITabBarController's rebuildTabBar method to setViewControllers, if TabBarViewController's view is not load, it will call viewDidLoad again. In viewDidLoad, we call setViewControllers again, if the viewControllers parameter array's first vc is different with the setting in rebuildTabBar, it crashed.
could you please help to explain the reason, thank you.