gpt4 book ai didi

ios - 比较 UIViewController 实例

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

我正在构建一个带有自定义标签栏的应用程序。其中一个按钮不会选择相应的 View Controller ,而是会触发一些自定义代码(例如模态打开)。

我将 UITabBarController 子类化如下:

import UIKit

class MyTabBarController: UITabBarController, UITabBarControllerDelegate {

override func viewDidLoad() {
super.viewDidLoad()

delegate = self
}

func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
if (viewController == self.viewControllers[2]) {
return false
}

return true
}
}

但是,比较viewController == self.viewControllers[2]好像不行。我收到以下错误:

Could not find an overload for == that accepts the supplied arguments

作为 Swift 的新手,我找不到任何 Material 来解释如何比较对象的特定实例,或者这是否是正确的方法。

如何更改此代码以使其正常工作?

最佳答案

首先(让它工作):打开self.viewControllers:

if (viewController == self.viewControllers![2]) {
return false
}

为了更安全,请参阅 this answer

第二(使其正确):删除不需要的括号,这显然会给编译器带来问题:

if viewController == self.viewControllers![2] {
return false
}

这样您实际上会收到更有用的错误消息:

Cannot subscript a value of type '[UIViewController]?' with an index of type 'Int'

这会给你更好的提示是哪里出了问题。

关于ios - 比较 UIViewController 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31500412/

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