gpt4 book ai didi

ios - 将 UITabBarDelegate 设置为 UIViewController 时应用程序崩溃

转载 作者:行者123 更新时间:2023-11-28 16:07:24 24 4
gpt4 key购买 nike

当我尝试在 UIViewController 的子类中将 UITabBarDelegate 设置为 self 时,我的应用程序崩溃了。我有一个 UITabBarViewController,其中有几个链接到 View Controller 的 TabItem。其中一个 View Controller 是 HomeViewController。我在 HomeViewController 中有以下代码:

class HomeViewController: UIViewController, UITabBarDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.tabBarController?.tabBar.translucent = false
self.extendedLayoutIncludesOpaqueBars = true
self.tabBarController?.tabBar.delegate = self //This is causing crash
}
}

如果我删除 self.tabBarController?.tabBar.delegate = self 行,一切正常,我的 tabBar 的行为符合预期,但是当我重新添加该行时,我会遇到以下崩溃:

ibc++abi.dylib: terminating with uncaught exception of type NSException

不太确定如何解决这个问题。在 SO 上找到了一些其他答案,但他们似乎仍然不清楚使这项工作的过程是什么。

谢谢!

最佳答案

您的 UITabBarViewController 已经是您的 UITabBar 的委托(delegate)。不要让你的 viewController 成为你的 tabBarDelegate,而是使用你的 tabBarViewController 并将你的逻辑放在那里。

你可以这样做。在 tabBarViewController 的 didSelectItem 委托(delegate)方法中

override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem)
{
let index = tabBar.items?.indexOf(item)

if (index == /*the required index of HomeViewController*/)
{
let homeVC = self.viewControllers.objectAtIndex(index) as! HomeViewController
homeVC.myMethod()
}

}

关于ios - 将 UITabBarDelegate 设置为 UIViewController 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40079752/

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