预期结果
在自定义标签栏 Controller 中创建标签栏图标的通用私有(private)方法。应该成功地为每个指示的 Controller 绘制标签栏图标。
结果
失败
Cannot Call Value of Non-Function Type 'UICollectionView'
失败(通用)代码:
private func createNavController(imageName: String, _ controllerName: UICollectionViewController) -> UINavigationController {
let layout = UICollectionViewFlowLayout()
Thrown Here-> let viewController = controllerName(collectionViewLayout: layout)
let navController = UINavigationController(rootViewController: viewController)
navController.tabBarItem.image = UIImage(named: imageName)
return navController
}
工作(非通用)代码
let userLayout = UICollectionViewFlowLayout()
let userController = UserController(collectionViewLayout: userLayout)
let navController = UINavigationController(rootViewController: userController)
navController.tabBarItem.image = UIImage(named: "loc-map-route")
相关
class UserController : UICollectionViewController, UICollectionViewDelegateFlowLayout
{
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = UIColor.green
}
}
环境
ios 10 swift 3 xcode 8
谢谢。
最佳答案
在您对 createNavController
的定义中您的参数controllerName
是 UICollectionViewController
类型这不是函数类型,因此您不能将其作为函数调用,即 controllerName(...)
.
如果你更换
let viewController = controllerName(collectionViewLayout: layout)
和
let viewController = UserController(collectionViewLayout: layout)
从您的工作代码中,它编译得很好。此外,您不需要
controllerName
参数了。反正我不太明白这是干什么用的。您是否希望能够指定 Controller 类型?然后你可以像 dan 建议的那样做(虽然这段代码需要 UICollectionViewController
因为你将使用 init(collectionViewLayout:)
)。您还可以像这样传递要嵌入到导航 Controller 中的 View Controller 的实例:
private func createNavController(imageName: String, rootViewController: UIViewController) -> UINavigationController
{
let navController = UINavigationController(rootViewController: rootViewController)
navController.tabBarItem.image = UIImage(named: imageName)
return navController
}
并这样称呼它:
let navController = createNavController(
imageName: "loc-map-route",
rootViewController: UserController(collectionViewLayout: UICollectionViewFlowLayout()))
这确实不需要
UICollectionViewController
因为UINavigationController.init(rootViewController:)
需要 UIViewController
.这一切都取决于您想要达到的目标。
关于ios - 在 Swift 3 中无法调用非函数类型 'UICollectionView' 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42837473/